When running the below C# HttpClient PostAsync request I am getting a bad request status. I'm not sure why this is. I have a username and password that I am passing. Not sure whether I am adding that correctly. New to this.
using (var client = new HttpClient())
{
var data = "{'Locations' : '[50.452603, 30.522025]'}";
var endpoint = new Uri("https://api.connect.test.com/risk/v3/countr/pop/location?");
var newPostJson = JsonConvert.SerializeObject(data);
var payload = new StringContent(newPostJson, Encoding.UTF8, "application/json");
var byteArray = Encoding.ASCII.GetBytes("username:password");
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
var status = client.PostAsync(endpoint, payload).Result.StatusCode.ToString();
Console.WriteLine(status);
}
First of all
data
is not a valid json.It should be
This way, you can pass it directly to
StringContent
.If you really want to use that
JsonConvert.Serialize
, you can make a POCO to store the data.