A
A
Alexander Petrov2018-02-08 21:00:22
ruby
Alexander Petrov, 2018-02-08 21:00:22

How to insert header in post request?

I'm trying to figure out the Avito API.
https://autoload.avito.ru/format/api/#section/Obsh...
Here you need to somehow insert the token into the header. Google-google can not figure out how to do it.
I get the token without problems:

require "net/http"
require "uri"
require 'json'

uri_token = URI.parse("https://api.avito.ru/token/")
response_token = Net::HTTP.post_form(uri_token, {"grant_type" => "client_credentials", "client_id" => "client_id", "client_secret" => "client_secret"})
result=JSON.parse(response_token.body)

token=result['access_token']

But how to insert it into the header for further requests is not clear. Help me please!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Petrov, 2018-02-08
@Mirkom63

In short, I found the solution myself. If someone needs it, then here:
uri = URI(" https://api.avito.ru/autoload/v1/accounts/{id_user... ")
res = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
req = Net::HTTP::Get.new(uri)
req['Authorization'] = 'Bearer '+token
concat http.request(req).body
end

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question