How to use x-www-form-urlencoded in rails
Asked Answered
G

4

11

Am trying to access the token from ExactOnlineAPI but the documentation recommends to only use x-www-form-urlencoded. Does Ruby on Rails has this kind of encoding, if so how can i use it.

What is the different between x-www-form-urlencoded and encode_www_form

 params =  {
             :code => "#{code}",
             :redirect_uri => '/auth/exact/callback',
             :grant_type   => "authorization_code",
             :client_id   => "{CLIENT_ID}",
             :client_secret => "CLIENT_SECRET"
           }
uri = URI('https://start.exactonline.nl/api/oauth2/token')
#
uri.query = URI.encode_www_form(params)
res = Net::HTTP.get_response(uri)
puts "Access Token: "+res.body
Gaeta answered 10/12, 2015 at 14:0 Comment(0)
A
17

Request bodies are defined by a form’s markup. In the form tag there is an attribute called enctype, this attribute tells the browser how to encode the form data. There are several different values this attribute can have. The default is application/x-www-form-urlencoded, which tells the browser to encode all of the values.

so when we want to send data to submit the form by those data as a params of the form the header will send application/x-www-form-urlencoded for define enctype

http.set_form_data(param_hash)

For your

params =  {
         :code => "#{code}",
         :redirect_uri => '/auth/exact/callback',
         :grant_type   => "authorization_code",
         :client_id   => "{CLIENT_ID}",
         :client_secret => "CLIENT_SECRET"
       }
  uri = URI('https://start.exactonline.nl/api/oauth2/token')
  #

  Net::HTTP::Get.new(uri.request_uri).set_form_data(params)

or for post request of form submission use Net::HTTP::Post

and encode_www_form is:

It Generate URL-encoded form data from given enum.

URI.encode_www_form([["name", "ruby"], ["language", "en"]])
#=> "name=ruby&language=en"

in your case

uri.query = URI.encode_www_form(params)
#=> "code=aas22&redirect_uri=...."

More info Here

Arnelle answered 10/12, 2015 at 14:26 Comment(3)
Hey Raj, Am having a problem to implement the example you gave me. Could you please write for me a clear example depending on the example I posted.Gaeta
yes for your api you need to use http.set_form_data({params})Arnelle
Hey Raj, I meant an example like for KingChintz. Could you please post it.Gaeta
E
6

To put it in simple terms, if you need to POST a application/www-url-form-encoded request:

# prepare the data:
params = [ [ "param1", "value1" ], [ "param2", "value2" ], [ "param3", "value3" ] ]

uri = ( "your_url_goes_here" )

# make your request:
response = Net::HTTP.post_form( uri, params )
if( response.is_a?( Net::HTTPSuccess ) )
    # your request was successful
    puts "The Response -> #{response.body}"
else
    # your request failed
    puts "Didn't succeed :("
end
Eckel answered 11/12, 2015 at 4:18 Comment(0)
G
2

If you're working with a Net::HTTP object and therefore can't use the post_form class method, encode the form values yourself, and provide the encoded value as the data string.

def post_form(path, form_params)
  encoded_form = URI.encode_www_form(form_params)
  headers = { content_type: "application/x-www-form-urlencoded" }
  http_client.request_post(path, encoded_form, headers)
end

def http_client
  http_client = Net::HTTP.new(@host, @port)
  http_client.read_timeout = @read_timeout
  http_client.use_ssl = true
  http_client
end

This is what Net::HTTP.post_form does internally.

Gallardo answered 20/3, 2019 at 15:8 Comment(0)
R
0

After many efforts, I was able to retrieve the token from some other server. Here is my code:

require 'net/http'
require 'json'

code = "the server should give you this at the end of the authorization phase"
redirect_uri = "this should be a URL to your own server"
token_uri = URI("this should be a URL to the token server")

https = Net::HTTP.new(token_uri.host, token_uri.port)
https.use_ssl = true

req = Net::HTTP::Post.new(token_uri.path)
params = {
  "token_content_type" => "jwt", # This is required by the server
                                 # I was working with. It may not 
                                 # be required by some other servers.
  "grant_type" => "authorization_code",
  "redirect_uri" => redirect_uri,
  "code" => code
}
req.body = URI.encode_www_form(params)
req['content_type'] = 'application/x-www-form-urlencoded'
req.basic_auth(client_id, client_secret)

res = https.request(req)
json = JSON.parse(res.body)
access_token = json["access_token"]

print(access_token)

I hope this is usefull.

Rudolphrudwik answered 6/7 at 21:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.