0

I have a HttpClient that I am using to use a REST API. However I am having trouble setting up the Authorization header. I need to set the header to the token I received from signin method, but when I use it in another method I get an authentication error.

var invoiceObj = new InvoiceUploadObj { dataFile = file, credential = "", domain = "" };
var invoiceObjSerialized = JsonConvert.SerializeObject(invoiceObj);
var data = new StringContent(invoiceObjSerialized, Encoding.UTF8, "application/json");

HttpClient clientDemoWS = new HttpClient();
clientDemoWS.BaseAddress = new Uri("https://www.nameservice.com");
clientDemoWS.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
clientDemoWS.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Authorization", String.Format("Bearer {0}", bearer_token));

HttpResponseMessage responseUpload = clientDemoWS.PostAsync("/services/invoice/upload", data).Result;

The status code is 200, but when I deserialize the server response I get an authentication error as error description.

Checking with Fiddler I see that this is the header:

POST /services/invoice/upload HTTP/1.1
Accept: application/json
Authorization: Bearer eyJ0eXAi......
Content-Type: application/json; charset=utf-8
Host: www.nameservice.com
Content-Length: 4623
Expect: 100-continue

Where am I wrong?

franksnet
  • 1
  • 1
  • It looks right to me. Maybe you need to ask the people who run the API you are connecting to. – Gabriel Luci Nov 17 '18 at 04:45
  • [This post](https://stackoverflow.com/questions/10077237/httpclient-authentication-header-not-getting-sent) looks like it has some solutions that may help you. – Tom F Nov 17 '18 at 06:41

0 Answers0