0
votes

I'm trying to create the following post using HttpClient, using postman its working correctly but I cant seem to replicate it in code. I need to set the header Content-Type to application/json and have an object in the body of the post.

POST https://mycompanyurl.com/authenticate

HEADERS
Key: Content-Type, Value: application/json

BODY { "username": "someusername", "password": "somepassword" }

using (var client = new HttpClient())
{
    client.BaseAddress = new Uri("https://companyurl.com");

    var serializedObject = JsonConvert.SerializeObject(
                          new {username = "username", password = "password" });
    var request = new HttpRequestMessage(HttpMethod.Post, "authenticate");
    request.Content = new StringContent(serializedObject, Encoding.UTF8,"application/json");                

    var response = await client.SendAsync(request);
    response.EnsureSuccessStatusCode();

    var content = await response.Content.ReadAsStringAsync();

}

Using the reverse proxy in fidder I can capture the raw call from postman which works, the rest api returns a result as expected:

POST http://127.0.0.1:8888/v1/authenticate HTTP/1.1 Content-Type: application/json;charset=UTF-8 cache-control: no-cache Postman-Token: 4db8f2dd-cbf0-413c-ad5b-20af0543a31d User-Agent: PostmanRuntime/7.6.0 Accept: / Host: 127.0.0.1:8888 accept-encoding: gzip, deflate content-length: 87 Connection: keep-alive

{"username":"username","password":"password"}

My call from HttpClient and using fiddler is below, This does not work, returns 200 but its not working correctly, data is not being returned, I cant see anything differences in the payload that will make the rest api not respond as expected.

POST http://127.0.0.1:8888/v1/authenticate HTTP/1.1 Content-Type: application/json; charset=utf-8 Host: 127.0.0.1:8888 Content-Length: 87 Expect: 100-continue Connection: Keep-Alive

{"username":"username","password":"password"}

1
Welcome to Stack Overflow! It's not fully clear here what kind of question you are asking. Are you getting any errors? Are there some parts of the code that is not working? Please read how to ask a question.ZarX
the header Content-Type is blank, its not being setSteve Jones

1 Answers

1
votes

The logic below should generate the same working request signature provided in your example (which was posted as an Answer, please edit your Question instead), and therefore should work:

var clientHandler = new HttpClientHandler
{
    AutomaticDecompression = System.Net.DecompressionMethods.GZip | System.Net.DecompressionMethods.Deflate,
    AllowAutoRedirect = false
};

using (var client = new HttpClient(clientHandler, true))
{
    client.BaseAddress = new Uri("http://127.0.0.1:8888/v1/");

    client.DefaultRequestHeaders.Add("cache-control", "no-cache");
    client.DefaultRequestHeaders.Add("Postman-Token", "db8f2dd-cbf0-413c-ad5b-20af0543a31d");
    client.DefaultRequestHeaders.Add("User-Agent", "PostmanRuntime/7.6.0");
    client.DefaultRequestHeaders.Add("Accept", "*/*");
    client.DefaultRequestHeaders.ExpectContinue = false;

    var serializedObject = JsonConvert.SerializeObject(
        new { username = "username", password = "password" }
        );

    var request = new HttpRequestMessage(HttpMethod.Post, "authenticate")
    {
        Content = new StringContent(serializedObject, Encoding.UTF8, "application/json")
    };

    var response = await client.SendAsync(request);

    response.EnsureSuccessStatusCode();

    var content = await response.Content.ReadAsStringAsync();
}

It will create the following request:

POST http://127.0.0.1:8888/v1/authenticate HTTP/1.1
cache-control: no-cache
Postman-Token: db8f2dd-cbf0-413c-ad5b-20af0543a31d
User-Agent: PostmanRuntime/7.6.0
Accept: */*
Content-Type: application/json; charset=utf-8
Host: 127.0.0.1:8888
Content-Length: 45
Accept-Encoding: gzip, deflate
Connection: Keep-Alive

{"username":"username","password":"password"}

Hope this helps.