Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Which the correct way to implement an API request

I’m trying to create a request like that, but with ruby.

curl --request POST \
--url '{BASE_URL}/oauth2/token' \
--header 'Authorization: Basic {BASIC_CLIENT}' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data grant_type=client_credentials

Im trying like this:

require 'uri'
require 'net/http'

url = URI("https://api.userede.com.br/redelabs/oauth2/token")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true

request = http.post(
  url,
  { 'grant_type' => 'client_credentials' }.to_json,
  { 'Authorization' => "Basic #{CLIENTID}", 'Content-Type' => 'application/x-www-form-urlencoded'}
)

But i’m getting the following error:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{\"error_description\":\"OAuth 2.0 Parameter: grant_type\",\"error\":\"invalid_request\",\"error_uri\":\"https://datatracker.ietf.org/doc/html/rfc6749#section-5.2\"}

>Solution :

You’re formatting the request as JSON, but the system is expecting application/x-www-form-urlencoded.

Replace this:

{ 'grant_type' => 'client_credentials' }.to_json,

with this:

"grant_type=client_credentials",

to conform to the correct payload type.

That may not be the only error, but it is an obvious one.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading