I cannot get the following POST request to work. It returns with status message 400 (Bad Request), no matter what I try.
static HttpClient client = new HttpClient();
client.BaseAddress = new Uri("<<REDACTED>>");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
// authorisation credentials go here, which work
var model = JsonConvert.SerializeObject(dialApi); //, Encoding.UTF8, "application/json"); //.DeserializeObject<Hello>(result);
var model1 = new StringContent(model, Encoding.UTF8, "application/json");
var x = "{\"data\":{\"Dest\":\"<<REDACTED>>\"}}";
//HttpResponseMessage response = await client.PostAsJsonAsync(path, model1);
HttpResponseMessage response = await client.PostAsJsonAsync(path, new StringContent(x, Encoding.UTF8, "application/json"));
The problem could be that the 'Content' -> 'Headers' --> 'ContentType' is 'text/html' (it should be 'application/json', but I cannot seem to change this. The default headers are already setup to 'application/json'
I should add that I have the API working in Postman, with the body sent as 'application/json' with the raw setting:
I wonder if anyone can help?
Update:
I have tried the alternative implementation given in the answer of the 'duplicate' question, which also does not work:
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, path);
request.Content = new StringContent("{\"data\":{\"Dest\":\"<<>>redacted\"}}",
Encoding.UTF8,
"application/json");//CONTENT-TYPE header
await client.SendAsync(request)
.ContinueWith(responseTask =>
{
Console.WriteLine("Response: {0}", responseTask.Result);
});
With the following printed to the console: