I'm trying to convert some code from HTTParty
to Faraday
. Previously I was using:
HTTParty.post("http://localhost/widgets.json", body: { name: "Widget" })
The new snippet is:
faraday = Faraday.new(url: "http://localhost") do |config|
config.adapter Faraday.default_adapter
config.request :json
config.response :json
end
faraday.post("/widgets.json", { name: "Widget" })
Which results in: NoMethodError: undefined method 'bytesize' for {}:Hash
. Is it possible to have Faraday automatically serialize my request body into a string?
The middleware list requires it be constructed/stacked in a particular order, otherwise you'll encounter this error. The first middleware is considered the outermost, which wraps all others, so the adapter should be the innermost one (or last):
Faraday.new(url: "http://localhost") do |config|
config.request :json
config.response :json
config.adapter Faraday.default_adapter
end
See Advanced middleware usage for additional info.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With