How to debug/display request sent using RestClient
Asked Answered
N

5

43

I am trying to use RestClient to access a webservice using post method. I am sending the authorization token as specified but I am still getting a 403 status error which means I am forbidden to use that api. Is there any way that I can see the request being sent with http post so that I can verify the headers? I am not able to find any example or any documentation which mentions as to how to do that?

My code is similar to this :

token = get_token

response = RestClient.post "https://api-dev.xxx.com/software/services/search/ABC",
  :authorization => "Bearer #{token}"
Nutgall answered 5/5, 2014 at 1:14 Comment(0)
R
30

You could try enabling RestClient's logging and see whether this provides any useful output:

RESTCLIENT_LOG=stdout path/to/my/program

or if you are using Rails

RESTCLIENT_LOG=stdout bundle exec passenger

replacing passenger with your choice of server. This will redirect all logging to standard output (your console).

Personally I prefer using more verbose tools when I need to inspect or troubleshoot HTTP requests.

You could try curl or wget if you prefer command-line, or one of the many browser extensions which let you perform requests easily, inspect output, save for future use, set up different environments etc. Both Postman and Advanced REST Client are good choices.

Retro answered 5/5, 2014 at 8:5 Comment(3)
Just because it was confusing for me at first, if you're using heroku, this is also the way to do it. Just use heroku config:set RESTCLIENT_LOG=stdoutIndoor
What is path/to/my/program here? If I use the path to my project the command fails saying: "is a directory..." Any help is appreciatedCalla
@halbano: This is a shell syntax defining an environment variable only in the given command. Here path/to/my/program should be the shell command to use to run your application.Heliport
N
72

If you're doing more of a REPL kind of development, it can be as easy as adding

RestClient.log = 'stdout'

to your code.

You can find other valid values in the docs.

Nichy answered 11/8, 2015 at 0:2 Comment(1)
I don't see the body of the POST request.Shadrach
R
30

You could try enabling RestClient's logging and see whether this provides any useful output:

RESTCLIENT_LOG=stdout path/to/my/program

or if you are using Rails

RESTCLIENT_LOG=stdout bundle exec passenger

replacing passenger with your choice of server. This will redirect all logging to standard output (your console).

Personally I prefer using more verbose tools when I need to inspect or troubleshoot HTTP requests.

You could try curl or wget if you prefer command-line, or one of the many browser extensions which let you perform requests easily, inspect output, save for future use, set up different environments etc. Both Postman and Advanced REST Client are good choices.

Retro answered 5/5, 2014 at 8:5 Comment(3)
Just because it was confusing for me at first, if you're using heroku, this is also the way to do it. Just use heroku config:set RESTCLIENT_LOG=stdoutIndoor
What is path/to/my/program here? If I use the path to my project the command fails saying: "is a directory..." Any help is appreciatedCalla
@halbano: This is a shell syntax defining an environment variable only in the given command. Here path/to/my/program should be the shell command to use to run your application.Heliport
M
2

In case you don't know (or want to bother) to pass in env. variable to your app (in my case it was Passenger/Rails), do something similar:

$ cat >/usr/share/foreman/config/initializers/00_rest_client.rb <<'EOT'
require 'rest_client'
RestClient.log =
  Object.new.tap do |proxy|
    def proxy.<<(message)
      Rails.logger.info message
    end
  end
EOT
Mica answered 14/4, 2015 at 7:30 Comment(0)
S
2

If you craft a request manually, you can use inspect to show the full url

req = RestClient::Request.new(
    :method => :post,
    :url => "https://api-dev.xxx.com/software/services/search/ABC",
    headers: {params:{:authorization => "Bearer #{token}"}})

puts req.inspect
Solecism answered 22/3, 2018 at 15:52 Comment(0)
C
2

You can also get the request from the exception:

def bla(token)
  response = RestClient.post "https://api-dev.xxx.com/software/services/search/ABC", :authorization => "Bearer #{token}"
rescue RestClient::Exception => e
   @logger.error "#{e.response.request.inspect}"
end 
Casiecasilda answered 8/4, 2019 at 10:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.