1
votes

i have to call an api in my c# class with httpclient. API need the content-type header, i want to get the response as json so i add content-type : application/json to headers in postman and do the post request and it works perfect : enter image description here

But if i write something else in content-type api returns html code. I have to do the exactly same thing as postman in C# Here is my example code :

            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri("adress");
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "adress");
            request.Content = new StringContent(myjson, Encoding.UTF8, "application/json");
            var y = await client.SendAsync(request);
            var x = await y.Content.ReadAsStringAsync();

But the result is always HTML not json.

1
Please dont hit me :s ... is the HTML page you see an error page?nilsK

1 Answers

1
votes

Interesting but solved in this way :

request.Content = new StringContent(myjson, Encoding.UTF8);
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
request.Content.Headers.Add("No-Paging", "true");