I am able to use Postman to make a successful Post a request with content type x-www-form-urlencoded to a REST API.
However, when I try to make the same post in a Blazor Server-Side app, I get back a 401 error.
The relevant part of my code is as follows:
FormUrlEncodedContent content = new(new Dictionary<string, string>()
{
["qualification_id"] = qualification.QualificationId,
["period_id"] = qualification.PeriodId,
["taken_date"] = qualification.CompletedDateString,
["expiration_date"] = qualification.ExpirationDateString,
});
content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
var request = new HttpRequestMessage(HttpMethod.Post, requestUri);
request.Headers.Add("x-api-key", xApiKey);
var client = _clientFactory.CreateClient();
var response = await client.PostAsync(requestUri, content);
var ret = await response.Content.ReadFromJsonAsync<MemberQualification>();
return ret;
Any suggestions would be greatly appreciated. NB - the API seems to return a 401 error for just about anything including, I think errors that should be 400 - and I am confident that my x-api-key credentials are correct and have been used successfully elsewhere in other httpClient calls.
Note that request
is created and filled but not used in any way. That matches the return code (401 Not authorized).
I think you can just use
//request.Headers.Add("x-api-key", xApiKey);
content.Headers.Add("x-api-key", xApiKey);