Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate Robust C# Code #104

Open
bjartur opened this issue Nov 28, 2017 · 2 comments
Open

Generate Robust C# Code #104

bjartur opened this issue Nov 28, 2017 · 2 comments

Comments

@bjartur
Copy link

bjartur commented Nov 28, 2017

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: postmanlabs/postman-app-support#3870

@darrenjennings
Copy link
Contributor

I'm not familiar enough with c# RestSharp library here to know if this is common, but I would expect your snippet to also specify somewhere that you're adding the parameter as application/x-www-form-urlencoded otherwise if it's a POST it would be sent as form-data?

@bjartur
Copy link
Author

bjartur commented Mar 27, 2019

On a POST or PUT Requests, it depends on whether or not you have files attached to a Request. —Official Documentaiton

The RestSharp library defaults to applicaiton/x-www-form-urlencoded and implicitly switches to multipart/form-data if the library is told to read a part of the request from a file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants