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
What's The Most Efficient Way Get The First Day of The Current Month
Undefined Method 'Reset' for Rdoc::Toplevel:Class When Installing a New Ruby Gem
Find Out Which Gems Require Native C Extensions from a Gemfile
How to Assert That No Route Matches in a Rails Integration Test
How to Combine a Series of PDFs into One Using Ruby
Standalone Ruby -- How to Load Different Environments from Database.Yml
Robustly Call a Flaky API: Proper Error Handling with Net::Http
How to Have One Resource in Routes for Namespace and Root Path Altogether - Rails 4
How to Find .Index of a Multidimensional Array
How to Daemonize Rails Rake Task on Elastic Beanstalk Start Up
Ruby: How to Load .Rb File in The Local Context
Rbenv and Bundler: "Bad Interpreter: No Such File or Directory"
Alternative for Accepts_Nested_Attributes_For - Maybe Virtus
How to Split a Directory String in Ruby