0

I am trying to call an API using HttpClient.PostAsync and I am getting Bad Request. I checked Fiddler and I see that the body request is being encoded and Transfer-Encoding: Chunked in the header.

response = Client.PostAsync(url, toSend, mediaTypeFormatter).Result;

toSend is a class

[DataContract]
public class OAuthRequestBody
{
    [DataMember(Name = "grant_type")]
    public string GrantType { get; set; }
    [DataMember(Name = "tpl")]
    public string Tpl { get; set; }
    [DataMember(Name = "user_login_id")]
    public string UserLogin { get; set; }
}

In this case, the Mediaformatter is Json but it can be different and that's why we are not using JsonFormatter.

When I check the textView in Fiddler I see this:

 66
{"grant_type":"client_credentials","tpl":"{number}","user_login_id":"x"}
0

I don't understand where the 66 and 0 are coming from.

The Raw Headers:
POST http://example.com/token
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: application/json; charset=utf-8
Accept: application/json
Accept-Encoding: gzip, deflate
Authorization: Basic FancyLongBase64
Host: example.com

66
{"grant_type":"client_credentials","tpl":"{number}","user_login_id":"x"}
0

I tried multiple ways but the encoding seems to be always there. Maybe there is some configuration.

Building the request:

var body = new OAuthRequestBody
                {
                    GrantType = "client_credentials",
                    Tpl = Tpl,
                    UserLogin = "1"
                };

mediaTypeFormatter = new JsonMediaTypeFormatter();

url = "/token" //Not the actual path but same idea
Zein Sleiman
  • 254
  • 2
  • 11
  • Can you please mention how you are constructing url, toSend, mediaTypeFormatter parameters as the definition for HttpClient.PostAsync is HttpClient.PostAsync Method (String, HttpContent)--https://msdn.microsoft.com/en-us/library/hh138242(v=vs.118).aspx – MBB Sep 11 '18 at 10:22
  • I updated the question. I am using HttpClientExtension. [msdn](https://msdn.microsoft.com/en-us/library/jj128123(v=vs.118).aspx) [Github](https://github.com/aspnet/AspNetWebStack/blob/master/src/System.Net.Http.Formatting/HttpClientExtensions.cs) You can view the code here. – Zein Sleiman Sep 11 '18 at 16:56
  • Check whether you added client.DefaultRequestHeaders.TransferEncodingChunked=true;try explicitly setting - client.DefaultRequestHeaders.TransferEncodingChunked=false; and try! – MBB Sep 11 '18 at 19:05
  • The reason for asking it to change in above comment is the request should not fail due to transport is chunked. Just to check without chunked it will get succeed or not? – MBB Sep 12 '18 at 04:31
  • Yes it will work. I took the same request from Fiddler and remove the encoding and the chuncked and it works. If you set the TransferEncoding to false, it still sets it as true. I will try again. – Zein Sleiman Sep 12 '18 at 16:06
  • Please find the details her -https://stackoverflow.com/questions/35464233/how-to-disable-chunked-transfer-encoding-in-asp-net-c-sharp-using-httpclient – MBB Sep 12 '18 at 19:04
  • show how the HttpClient is created and how the header are set! – MBB Sep 12 '18 at 19:35

0 Answers0