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?