1

I have an API end-point that I need to post to.

Using Postman I do the following:

  1. Set the method to be POST
  2. Add the URL
  3. In the Headers I set: Content-Type to be application/json
  4. In the Body I add my json string
  5. I hit [Send] and get a 200 response with the expected response.

However, in C# .Net Framework 4.8 (LinqPad 5) I do

var c = new HttpClient();  // Disposed of later
c.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

string data = @"{ ""a"": ""b"", ""c"": ""d"" }"; // copied from Postman.
HttpContent payload = new StringContent(data, Encoding.UTF8, "application/json");

var msg = new HttpRequestMessage(HttpMethod.Post, new Uri("https://the.url"), UriKind.Absolute)
{
    Content = payload,
};    

var response = c.SendAsync(msg).GetAwaiter().GetResult(); // It's a synchronous flow

And this responds with a 502 Bad Gateway.

What am I missing...?

I should point out that I need to use the HttpClient and not RestSharp.

DrGriff
  • 4,394
  • 9
  • 43
  • 92

0 Answers0