Kong / Kong/httpsnippet

Generate Robust C# Code

未關閉
#104 2 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視
主要語言
TypeScript
星號
1.2k
分支
242
PR 合併指標
30 天內沒有已合併 PR

描述

## 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 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

貢獻指南

開啟貢獻指南

研究方向

Start at the C# snippet-generation entry point and compare the current form-urlencoded output with the issue’s two examples, focusing on how parameter names and values are represented. Done means generated C# uses request parameters with runtime-safe URL encoding rather than embedding one encoded request-body string.

由索引模型根據 Issue 內容生成。

評估

技術堆疊
csharp, typescript
領域
devtools, tooling
Issue 類型
功能
難度
3/5
預估耗時
1-2 天
活躍度
停滯
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。