简体   繁体   中英

How to Post to restful web API

I am trying to post to my API that accepts JSON format data

I however get a 406 not acceptable error.

HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
var user = "demo";
var password = "demo";
var base64String = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{user}:{password}"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", base64String);
string url = "https://api.fever.co.za/FTIntegration.svc/BalanceLookup";
var data = new
{
    BalanceID = "4E45D053-044E-4C7E-A2A3-0743A7237811",
    CardOrIDNumber = "8212225222075"
};
var stringContent = new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json");


var response = await client.PostAsJsonAsync(url, stringContent );

What could be the cause of this error?

I think this problem may be solved by this response: https://stackoverflow.com/a/14252326/6996150

Your operation did not fail.

Your backend service is saying that the response type it is returning is not provided in the Accept HTTP header in your Client request.

Ref: http://en.wikipedia.org/wiki/List_of_HTTP_header_fields

  • Find out the response (content type) returned by Service.
  • Provide this (content type) in your request Accept header.

http://en.wikipedia.org/wiki/HTTP_status_code -> 406

(credits to @TheWhiteRabbit)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM