Home > Net >  Ruby Faraday gem adds Content-type when no body
Ruby Faraday gem adds Content-type when no body

Time:03-29

I am using the Ruby Faraday gem to make some requests to the Bitstamp API.

I want to make a this post request (to get my account balance) with an empty body. The problem that I'm having is that Faraday adds a Content-type header during the execution of the request, I suppose, in a middleware. The Bitstamp API doesn't permit the inclusion of the Content-type header when the body is empty, so I'm getting the following error:

BitstampClient::Error: Bitstamp error: 403 {"status": "error", "reason": "Content-Type header should not be present", "code": "API0020"

How can i exclude the Content-type header? I tested the same request in Postman, and it worked well. So i'm sure that this is the problem.

CodePudding user response:

My builder is the following: <Faraday::RackBuilder:0x000000401e9f2208 @adapter=Faraday::Adapter::NetHttp, @handlers=[Faraday::Request::UrlEncoded]>

Yes, the UrlEncoded middleware adds the Content-Type header. Docs:

The middleware also automatically sets the Content-Type header to application/x-www-form-urlencoded. https://github.com/lostisland/faraday/blob/8f00640dd51da00c8ca09f29bca0a2b32c553925/docs/middleware/request/url_encoded.md

  • Related