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

Postman generates invalid C# code #12799

Open
1 task done
derekantrican opened this issue Apr 15, 2024 · 0 comments
Open
1 task done

Postman generates invalid C# code #12799

derekantrican opened this issue Apr 15, 2024 · 0 comments

Comments

@derekantrican
Copy link

derekantrican commented Apr 15, 2024

Is there an existing issue for this?

  • I have searched the tracker for existing similar issues and I know that duplicates will be closed

Describe the Issue

When using "Code snippet" > "C# - HttpClient", the code generated is invalid and will not run.

Steps To Reproduce

Import the following curl request (I used a real request, but changed some values for the sake of privacy):

curl "https://myurl.com/endpoint" ^
  -H "accept: application/json, text/plain, */*" ^
  -H "accept-language: en-US,en;q=0.8" ^
  -H "authorization: Bearer MYTOKEN" ^
  -H "content-type: application/json;charset=UTF-8" ^
  -H "origin: https://origin.myurl.com" ^
  -H "referer: https://referer.myurl.com/" ^
  -H ^"sec-ch-ua: ^\^"Brave^\^";v=^\^"123^\^", ^\^"Not:A-Brand^\^";v=^\^"8^\^", ^\^"Chromium^\^";v=^\^"123^\^"^" ^
  -H "sec-ch-ua-mobile: ?0" ^
  -H ^"sec-ch-ua-platform: ^\^"Windows^\^"^" ^
  -H "sec-fetch-dest: empty" ^
  -H "sec-fetch-mode: cors" ^
  -H "sec-fetch-site: same-site" ^
  -H "sec-gpc: 1" ^
  -H "user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36" ^
  --data-raw ^"^{^\^"TeamIds^\^":^[57566^],^\^"StartTime^\^":^\^"2024-03-01T00:00:00.000Z^\^",^\^"EndTime^\^":^\^"2024-05-31T23:59:59.999Z^\^"^}^"

After importing the above request, generate a code snippet using "Code snippet" > "C# - HttpClient". The following snippet is generated:

var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post, "https://myurl.com/endpoint");
request.Headers.Add("accept", "application/json, text/plain, */*");
request.Headers.Add("accept-language", "en-US,en;q=0.8");
request.Headers.Add("authorization", "Bearer MYTOKEN");
request.Headers.Add("content-type", "application/json;charset=UTF-8");
request.Headers.Add("origin", "https://origin.myurl.com");
request.Headers.Add("referer", "https://referer.myurl.com/");
request.Headers.Add("sec-ch-ua", "\"Brave\";v=\"123\", \"Not:A-Brand\";v=\"8\", \"Chromium\";v=\"123\"");
request.Headers.Add("sec-ch-ua-mobile", "?0");
request.Headers.Add("sec-ch-ua-platform", "\"Windows\"");
request.Headers.Add("sec-fetch-dest", "empty");
request.Headers.Add("sec-fetch-mode", "cors");
request.Headers.Add("sec-fetch-site", "same-site");
request.Headers.Add("sec-gpc", "1");
request.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36");
var content = new StringContent("{\"TeamIds\":[57566],\"StartTime\":\"2024-03-01T00:00:00.000Z\",\"EndTime\":\"2024-05-31T23:59:59.999Z\"}", null, "application/json;charset=UTF-8");
request.Content = content;
var response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
Console.WriteLine(await response.Content.ReadAsStringAsync());

However, the above code is invalid in multiple ways and will not run.

Here is the correct equivalent code:

var client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); //Content-Type header must be specified in this way
var request = new HttpRequestMessage(HttpMethod.Post, "https://myurl.com/endpoint");
request.Headers.Add("accept", "application/json, text/plain, */*");
request.Headers.Add("accept-language", "en-US,en;q=0.8");
request.Headers.Add("authorization", "Bearer MYTOKEN");
request.Headers.Add("origin", "https://origin.myurl.com");
request.Headers.Add("referer", "https://referer.myurl.com/");
request.Headers.Add("sec-ch-ua", "\"Brave\";v=\"123\", \"Not:A-Brand\";v=\"8\", \"Chromium\";v=\"123\"");
request.Headers.Add("sec-ch-ua-mobile", "?0");
request.Headers.Add("sec-ch-ua-platform", "\"Windows\"");
request.Headers.Add("sec-fetch-dest", "empty");
request.Headers.Add("sec-fetch-mode", "cors");
request.Headers.Add("sec-fetch-site", "same-site");
request.Headers.Add("sec-gpc", "1");
request.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36");
var content = new StringContent("{\"TeamIds\":[57566],\"StartTime\":\"2024-03-01T00:00:00.000Z\",\"EndTime\":\"2024-05-31T23:59:59.999Z\"}", System.Text.Encoding.UTF8, "application/json"); //This is the proper way to specify encoding
request.Content = content;
var response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
Console.WriteLine(await response.Content.ReadAsStringAsync());

Screenshots or Videos

No response

Operating System

Windows

Postman Version

11.0.0

Postman Platform

Postman App

User Account Type

Signed Out User

Additional Context?

There are other ways to improve the C# code above (for instance, JsonContent instead of StringContent) but this is the minimal amount of changes to make the code useable

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

1 participant