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