I'm trying to make an HTTP POST request, I'm having problems adding an authorization in the header that has special characters like / and =
The code I use is the following
HttpRequestMessage request = new HttpRequestMessage(method, requestUri);
foreach (var h in headers)
{
request.Headers.Add(h.Key, h.Value);
}
And the exception I get
The format of value 'kAw4dhLDTHY8HvEZWZmv7k6/PHw=' is invalid."
StackTrace " at System.Net.Http.Headers.HttpHeaderParser.ParseValue(String value, Object storeValue, Int32& index)\r\n at System.Net.Http.Headers.HttpHeaders.ParseAndAddValue(HeaderDescriptor descriptor, HeaderStoreItemInfo info, String value) \r\n at System.Net.Http.Headers.HttpHeaders.Add(HeaderDescriptor descriptor, String value)\r\n at System.Net.Http.Headers.HttpHeaders.Add(String name, String value)\r\n
I tried several ways but I did not find a solution
I hope you can help me
Thank you very much
Cheers
First of all, the characters that you mention that give an error should not give. They are characters allowed for an HTTP header (in the example below I do it with the same value that you indicate that it gives an error)
Although it would be good if you could place part of the code, to see how you are putting together the headers and help you better.
Send help two ways to "Send" or "set" HTTP headers when making a request with HTTPClient in the POST method. (Here I use HttpClientFactory )
The two options are:
OPTION 1: Using the Post (or PostAsync) method
Here the headers are placed in the HttpClient object (because it would be good even if these Auth (Authorization) headers were configured globally (so as not to be repeating in each request or method). This can also be done. Here I leave an example of how to "arm" and set httpClient.DefaultRequestHeaders.Authorization with System.Net.Http.Headers.AuthenticationHeaderValue where it is with the same
OPTION 2: Using the Send (or SendAsync) method building an Http (your example seems to use this method) with the HttpRequestMessage object
Here the HttpRequestMessage object is used to assemble the message and send via
Links that can help you
All examples build on that example I had with POST call, and add example headers
I hope it helps or guides you.
In the end to solve it I had to change the method to the following
The Headers.Add method I don't know what type of validation it performs that didn't let me enter certain types of characters