I am trying to connect to an API using HttpClient but when I try to I get the error Cannot send a content-body with this verb-type.
I have copied the code from Postman where it works ok but not in a C# .net windows form app.
Also if I comment out the content.headers.ContentType
part I get this error
Response status code does not indicate success: 415 (Unsupported Media Type).
What Am I missing?
try
{
var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Get, "https://connect1.uat.westpac.co.nz/fxrates/v1/forexratesheet/v2");
request.Headers.Add("X-IBM-Client-Id", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx");
request.Headers.Add("X-IBM-Client-Secret", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
request.Headers.Add("accept", "application/json");
var content = new StringContent(string.Empty);
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
request.Content = content;
var response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
this.txtResult.Text = await response.Content.ReadAsStringAsync();
//Console.WriteLine(await response.Content.ReadAsStringAsync());
}
catch (Exception ex)
{
this.txtResult.Text = ex.Message;
}