Generate Robust C# Code

未关闭
#104 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
35/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
停滞
技术栈
csharp, typescript
领域
devtools, tooling

调研方向

从 C# 代码片段生成入口开始,将当前的 form-urlencoded 输出与 issue 中的两个示例进行比较,重点关注参数名称和值的表示方式。完成的标准是:生成的 C# 使用带有运行时安全 URL 编码的请求参数,而不是嵌入一个经过编码的请求体字符串。

由索引模型根据 Issue 内容生成。

描述

Improvement Suggestion:

I would have liked the following autogenerated C# for a HTTP POST request with the single parameter err of value + & serialized as x-www-form-urlencoded in the body.

var client = new RestClient("https://localhost");
var request = new RestRequest(Method.POST);
request.AddHeader("postman-token", "da4d0044-61a9-3943-40db-9e753aca6020");
request.AddHeader("cache-control", "no-cache");
Action<string,string> parameter = (name, value) => request.AddParameter(name, value);
parameter("err", "+ &");
IRestResponse response = client.Execute(request);

but what Postman did produce for me was the following C# code:

var client = new RestClient("https://localhost");
var request = new RestRequest(Method.POST);
request.AddHeader("postman-token", "da4d0044-61a9-3943-40db-9e753aca6020");
request.AddHeader("cache-control", "no-cache");
request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddParameter("application/x-www-form-urlencoded", "err=%2B%20%26", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);

Rationale

  1. parameter("err", "+ &"); is more readable than "[..] err=%2B%20%26 [..]". The value, + &, is more recognizable.
  2. The code is more maintanable. At work, I tried the following development workflow:
    (a) Type a minimal working example request into to Postman.
    (b) Generate C# code.
    (c) Copy generated code to Visual Studio.
    (d) Replace example values by C# method parameters.
    This workflow failed, because the method parameters were not x-www-form-urlencoded at runtime. Generating UrlEncode() calls would make writing a consumer of a HTTP POST x-www-form-urlencoded API effortless.
  3. This usage of IRestRequest.AddParameter is encouraged, whereas the currently generated code depends on an internal hack not meant for 3rd party usage.
    /// - RequestBody: Used by AddBody() (not recommended to use directly)

App Details:

Postman for Windows
Version 5.3.2
win32 6.1.7601 / x64

Continuation of: https://github.com/postmanlabs/postman-app-support/issues/3870

主要语言
TypeScript
星标
1.2k
派生
242
PR 合并指标
30 天内没有已合并 PR

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

Kong/httpsnippet 的其他 Issue

查看 Kong/httpsnippet 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。