Faraday Vs Httparty

Why the api call that works with HTTParty doesn't with Faraday?

It looks like you're using mislav's recent blog post about Faraday for a code sample. You'll notice that in his example, the code you used is only being used for a GET request, not a POST request. When he made a POST request earlier in the page, he set a specific Faraday::Request type.

Based on the discussion at github, moment requires that you post JSON data, so you need to use this:

builder.use Faraday::Request::JSON

ASCII ruby net/http changing request uri

, is a legal character in a query (extended clarification is here https://stackoverflow.com/a/31300627/3820185)

ERB::Util.url_encode instead does replace [^a-zA-Z0-9_\-.]:

# File erb.rb, line 930
def url_encode(s)
s.to_s.dup.force_encoding("ASCII-8BIT").gsub(/[^a-zA-Z0-9_\-.]/n) {
sprintf("%%%02X", $&.unpack("C")[0])
}
end

So when doing the request, most probably the query is re-parsed to conform to the actual standards.

EDIT

Also you don't need to use ERB::Util.url_encode at all, you can just pass your URL to URI, it will propery escape it according to the standards.

irb(main):001:0> require 'net/http'
=> true
irb(main):002:0> link = URI 'http://example.com?q=Hello, World'
=> #<URI::HTTP http://example.com?q=Hello,%20World>
irb(main):003:0>

Connecting to Coinbase API with Ruby and running into connection issue. Faraday

You can check the code at http/header.rb: it calls strip on every header, so every header value should be a string.

I think this is the offending line, you are supplying an integer here as the header value:

req.headers['CB-ACCESS-TIMESTAMP'] = Time.now.to_i

Converting it to string should fix it:

req.headers['CB-ACCESS-TIMESTAMP'] = Time.now.to_i.to_s


Related Topics



Leave a reply



Submit