Server-side Blazor Post using HttpClient with x-www-form-urlencoded

Roddy Balkan

I am able to use Postman to make a successful Post a request with content type x-www-form-urlencoded to a REST API. successful post

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.

Henk Holterman

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);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

POST using HttpClient fro content type application/x-www-form-urlencoded using grant_type and scope

how to post body x-www-form-urlencoded using webclient?

How to do a post request using FORM-DATA or x-www-form-urlencoded with Android?

How to do a x-www-form-urlencoded POST login using cypress?

How to force Angular2 to POST using x-www-form-urlencoded

POST application/x-www-form-urlencoded Body to REST API using Jitterbit

How do I post data using okhttp library with content type x-www-form-urlencoded?

How to POST with application/x-www-form-urlencoded header and URLSearchParams using isomorphic-fetch

POST using cURL and x-www-form-urlencoded in PHP returning Access Denied

Send sensitive data using POST+application/x-www-form-urlencoded

How to post (x-www-form-urlencoded) Json Data using Retrofit?

How to post a x-www-form-urlencoded data properly using javascript?

Trying to make an API POST using application/x-www-form-urlencoded in a wpf desktop app

How to post an array of values using `UrlFetchApp.fetch` with `application/x-www-form-urlencoded`?

Jersey client Post Request with x-www-form-urlencoded Fails

How to POST x-www-form-urlencoded in retrofit

Spring - wrong encoding POST request with x-www-form-urlencoded

Post a x-www-form-urlencoded request from React Native

How to send post request with x-www-form-urlencoded body

post application/x-www-form-urlencoded Alamofire

Swift 4 send POST request as x-www-form-urlencoded

RestSharp post request - Body with x-www-form-urlencoded values

How to POST content as application/x-www-form-urlencoded

How to send x-www-form-urlencoded in a post request in webclient?

Axios post form urlencoded requests application/x-www-form-urlencoded

How to add "application/x-www-form-urlencoded" as Content-type in .Net httpClient

Retrofit + POST method + www-form-urlencoded

Using HttpClient in Blazor server side getting StatusCode 407 AuthenticationRequired

How to convert x-www-form-urlencoded post Message to JSON post Message?

TOP Ranking

HotTag

Archive