1

I have an remote endpoint that requires basic auth and client_credentials in the grant_type.

In Postman I can see the headers and body look like this:

Request Headers:
  Content-Type: application/x-www-form-urlencoded
  Authorization: Basic <my Base64 encoded credentials>
  User-Agent: PostmanRuntime/7.15.2
  Accept: "*/*"
  Cache-Control: no-cache
  Postman-Token: <uuid>
  Host: <target url>
  Accept-Encoding: gzip, deflate
  Content-Length: 29
  Connection: keep-alive

Request Body:
  grant_type=client_credentials

If I remove the request body, the endpoint returns 401.

When I do the following with the ServiceStack HttpUtils:

var response = url.PostToUrl(
  formData: $"grant_type:\"client_credentials\", 
  requestFilter: req =>
  {
    req.AddBasicAuth(key, secret);
    req.ContentType = "application/x-www-form-urlencoded";
  });

I also get 401 Unauthorized exception, implying that the grant_type client_credentials is not being posted properly. What is needed to resolve this in code?

1 Answer 1

1

I quickly realised the body needs to be name value pairs, so this is what is needed to replicate what is being sent by postman:

var response = url.PostToUrl(
  formData: new { grant_type = "client_credentials" }, 
  requestFilter: req =>
  {
    req.AddBasicAuth(key, secret);
    req.ContentType = "application/x-www-form-urlencoded";
  });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.