Alexander Smith
Alexander Smith

Reputation: 157

Rails 5.1 - HTTP Post request for token authorization

I am new to Rails and having trouble making an HTTP Post request.

I want to simply make the POST with two parameters 'client_id' and 'client_secret' and the response will contain the authorization token and I would simply like to pass this token back to the previous function called.

This is my initial code using net/http

require 'net/http'
require 'uri'

def authorize
  uri = URI.parse('https://api.url.com/auth?client_id=' + ENV['CLIENT_ID'] + '&client_secret=' + ENV['CLIENT_SECRET'])

  res = Net::HTTP::Post.new(uri)

  return res.data.token
end

Upvotes: 1

Views: 597

Answers (1)

Alexander Smith
Alexander Smith

Reputation: 157

Worked it out from documentation.

def authorize
  uri = URI('https://api.url.com/2_1/auth')
  res = Net::HTTP.post_form(uri, 'client_id' => ENV['CLIENT_ID'], 'client_secret' => ENV['CLIENT_SECRET'])
  p res.body
end

Upvotes: 1

Related Questions