0

I need to make an HTTP request using HTTPClient. The web API requires that I add a custom header of this format:

HEADER: Authorization: consumer={consumerId}, consumerToken={consumerTokenValue}

Following is the Code:

string headr = "consumer=40816499,consumerToken=" + "test";
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Clear();
            
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("Authorization", headr);
var response =await client.PostAsJsonAsync<remitaData>(url, remd);
var chk = await response.Content.ReadAsStringAsync();

But I get this error message on execution:

The format of value 'consumer=40816499,consumerToken=test' is invalid.

Sh.Imran
  • 1,035
  • 7
  • 13
Udeme Bassey
  • 29
  • 1
  • 4

1 Answers1

0

You need to put a "type" at the start of the header. e.g.:

client.DefaultRequestHeaders.Add("Authorization", "custom " + headr);

(where the string "custom" is defined by the server).

See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Authorization (or

Moose Morals
  • 1,628
  • 25
  • 32
  • I do not understand can you please explain further because the link you gave was not helpful. Please note that my code runs perfectly on postman – Udeme Bassey Aug 16 '20 at 21:42