clement360
clement360

Reputation: 121

HTTP Post in ruby NoMethodError

I am trying to make a http post using require 'net/http' here is my request

token_url = 'http://www.[url].com/oauth/token'
uri = URI(token_url)
req = Net::HTTP::Post.new(uri)
req.basic_auth ENV['OAUTH_ID'], ENV['OAUTH_PASSWORD']
req.set_form_data({ 'grant_type' => 'client_credentials' })
resp = Net::HTTP.new(uri.host, uri.port).start {|http| http.request(req) }
JSON.parse(resp.body)['access_token']

I keep getting this error: NoMethodError - undefined method 'empty?' for #<URI::HTTP:0x007fb2ed01bdf8>:

This happens on this line "req = Net::HTTP::Post.new(uri)". any ideas why?

Upvotes: 1

Views: 1018

Answers (1)

clement360
clement360

Reputation: 121

Figured it out:

replace

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

with

req = Net::HTTP::Post.new(uri.request_uri)

Upvotes: 2

Related Questions