I'm trying to send a POST request to a Web API.
If I do it by Postman, it works:
NowwhenItrytoexecutethroughcode,Igetthefollowingerror:
{"status": false,
"message": "Content Type Inválido. (Formato aceito: Content Type = \"application/json\" ",
"total": 0
}
I've tried it this way:
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(Constants.APIV2);
client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");
client.DefaultRequestHeaders.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(Constants.APIAuth)));
string json = JsonConvert.SerializeObject(billing);
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, Constants.Billing);
request.Content = new StringContent(json, Encoding.UTF8, "application/json");
await client.SendAsync(request)
.ContinueWith(responseTask =>
{
var teste = responseTask.Result.Content.ReadAsStringAsync();
});
And this:
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(Constants.APIV2);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(Constants.APIAuth)));
string json = JsonConvert.SerializeObject(billing);
HttpResponseMessage response = await client.PostAsync(Constants.Billing, new StringContent(json, Encoding.UTF8, "application/json"));
if (response.IsSuccessStatusCode)
{
var result = response.Content.ReadAsStringAsync();
}
And to no avail.
Any idea what I might be doing wrong?