0

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?

Nkosi
  • 235,767
  • 35
  • 427
  • 472
  • I prefer to use restsharp (http://restsharp.org/) to post, get or any other rest api request. It is very simple and useful library. – Baris Dec 05 '18 at 08:04
  • 2
    Please read [MCVE] guidance on posting code. In particular half of your code is not used at all and you are missing server side portion. – Alexei Levenkov Dec 05 '18 at 08:04
  • updated the code and serialized once - still get the 406 error – Radhiyah Williams Dec 05 '18 at 08:20
  • You need to send another header `Accept` with value `application/json` – Chetan Dec 05 '18 at 08:22
  • @RadhiyahWilliams `PostAsJsonAsync` already does the serializing of the data for you. What you had originally with `var response = await client.PostAsJsonAsync(url, data);` should be fine. You have not shown the serverside of things which is probably where the issue is. – Nkosi Dec 05 '18 at 08:23
  • Try clearing the accept header before adding your desired type `client.DefaultRequestHeaders.Accept.Clear()` – Nkosi Dec 05 '18 at 08:26
  • tried that aswell error persists - the server side returns data in json format- i do not have access to the server just the credentials and endpoint url – Radhiyah Williams Dec 05 '18 at 08:38
  • @RadhiyahWilliams You stated that the API was your API. What is the content type of the current response. check that. – Nkosi Dec 05 '18 at 08:40
  • ("Content-Type", "application/json") – Radhiyah Williams Dec 05 '18 at 08:41

1 Answers1

0

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)

Nkosi
  • 235,767
  • 35
  • 427
  • 472
johey
  • 1,139
  • 1
  • 9
  • 25