Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Faraday not logging POST request body #254

Closed
alfius opened this issue Apr 9, 2013 · 11 comments
Closed

Faraday not logging POST request body #254

alfius opened this issue Apr 9, 2013 · 11 comments

Comments

@alfius
Copy link

alfius commented Apr 9, 2013

I'm using the following code to create a connection:

@connection = Faraday.new(HOST, ssl: { verify: true }) do |faraday|
  faraday.request :url_encoded
  faraday.response :logger
  faraday.adapter Faraday.default_adapter
end

Then, I'm performing a post in this way:

@connection.post do |request|
  request.url "#{my_uri.path}?#{my_uri.query}"
  request.headers['Content-Type'] = 'application/json'
  request.body = my_object.to_json
end

The request works fine. The problem is that I'm not seeing the request body in the logs, only the request url.

@mislav
Copy link
Contributor

mislav commented Apr 9, 2013

The logger doesn't do bodies. Maybe this can be an optional feature, but by default I don't think it should ever do bodies because they can be pretty big and would inflate the log very quickly.

@mtarnovan
Copy link

I think there are multiple use-cases where you would want to log the entire body. I need this too. Yes, bodies can be pretty big, but we can use log level debug and let the passed logger instance control the verbosity.

@mislav
Copy link
Contributor

mislav commented Oct 21, 2013

@mtarnovan Agreed. Follow the referenced pull request for updates. We might add the feature but it will have to be opt-in, and it will need to have some sort of guard to avoid dumping non-plaintext bodies (such as compressed or binary responses)

@DougPuchalski
Copy link

👍 Key for debugging

@henrik
Copy link

henrik commented Jul 9, 2015

Since I found this page through Google: this is now supported.

require "logger"

Faraday.new do |faraday|
  faraday.response :logger, ::Logger.new(STDOUT), bodies: true
end

There are also more fine-grained settings available if you need them. See the pull request.

@vemv
Copy link

vemv commented Mar 26, 2017

This doesn't seem to work for url_encoded requests?

@iMacTia
Copy link
Member

iMacTia commented Mar 27, 2017

Hi @vemv, the order of middlewares is extremely important in Faraday.
Can you please provide your conneciton initialization code and try to explain better why it "doesn't seem to work for url_encoded requests"?
What's the output that you get and what is the expected one?

@jpickwell
Copy link

I know this is old, but did it not occur to anyone that the OP is logging the response: faraday.response :logger.

@iMacTia
Copy link
Member

iMacTia commented Jul 1, 2017

@jpickwell you use faraday.response because the logger middleware is registered as a response middleware. In reality, it logs both requests and responses 😄

@davidalpert
Copy link

davidalpert commented Oct 25, 2020

just came across this; by this line it appears that you need to pass two args to see the request body:

Faraday.new do |faraday|
  faraday.response :logger, ::Logger.new(STDOUT), body: true, bodies: { request: true, response: true }
end

(unless that has changed in a more recent commit 🤔 )

@iMacTia
Copy link
Member

iMacTia commented Oct 26, 2020

@davidalpert that's not necessary, you can simply pass the bodies: true to enable them both, or use the separate values like in the example above to have more control.
I see there's a body: true option in that example, but that's not a valid option unfortunately (maybe they got the plural wrong?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants