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

NoMethodError: undefined method `bytesize' for #<Hash:0x007fb881de2c90> #161

Closed
ckhall opened this issue May 25, 2012 · 7 comments
Closed

Comments

@ckhall
Copy link

ckhall commented May 25, 2012

trace:

NoMethodError: undefined method `bytesize' for #<Hash:0x007fb881de2c90>
    from /Users/chall/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/http.rb:1932:in `send_request_with_body'
    from /Users/chall/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/http.rb:1919:in `exec'
    from /Users/chall/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/http.rb:1317:in `block in transport_request'
    from /Users/chall/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/http.rb:1316:in `catch'
    from /Users/chall/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/http.rb:1316:in `transport_request'
    from /Users/chall/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/http.rb:1293:in `request'
    from /Users/chall/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/http.rb:1286:in `block in request'
    from /Users/chall/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/http.rb:745:in `start'
    from /Users/chall/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/http.rb:1284:in `request'
    from /Users/chall/.rvm/gems/ruby-1.9.3-p194@my_api/gems/faraday-0.8.0/lib/faraday/adapter/net_http.rb:72:in `perform_request'
    from /Users/chall/.rvm/gems/ruby-1.9.3-p194@my_api/gems/faraday-0.8.0/lib/faraday/adapter/net_http.rb:35:in `call'
    from /Users/chall/.rvm/gems/ruby-1.9.3-p194@my_api/gems/faraday-0.8.0/lib/faraday/connection.rb:226:in `run_request'
    from /Users/chall/.rvm/gems/ruby-1.9.3-p194@my_api/gems/faraday-0.8.0/lib/faraday/connection.rb:99:in `put'
    from /Users/chall/.rvm/gems/ruby-1.9.3-p194@my_api/gems/fulcrum-0.0.1/lib/my_api/form.rb:31:in `update'
    from (irb):5
    from /Users/chall/.rvm/rubies/ruby-1.9.3-p194/bin/irb:16:in `<main>'

request params:

params = {"form"=>{"id"=>"4fbce262ab0a5d57f600022f", "name"=>"POI", "description"=>"", "created_at"=>"2012-05-23T13:13:06Z", "updated_at"=>"2012-05-23T13:13:06Z", "elements"=>[{"disabled"=>false, "hidden"=>false, "key"=>"b9195f6c-d9ff-9b61-838a-6f66a00b683f", "type"=>"TextField", "data_name"=>"name", "required"=>false, "label"=>"Name"}, {"disabled"=>false, "hidden"=>false, "allow_other"=>true, "key"=>"9e1d6d5e-157b-2992-d93f-aa65e4c8d2ff", "type"=>"ClassificationField", "classification_set_id"=>"4fbce262ab0a5d57f6000005", "data_name"=>"classification", "required"=>true, "label"=>"Classification"}, {"disabled"=>false, "hidden"=>false, "key"=>"b9829d7b-ed94-ca8a-3f28-9e063f959cb3", "type"=>"TextField", "data_name"=>"address_1", "required"=>false, "label"=>"Address 1"}, {"disabled"=>false, "hidden"=>false, "key"=>"07cc253d-56da-2f29-eaae-693b2bf4be5c", "type"=>"TextField", "data_name"=>"address_2", "required"=>false, "label"=>"Address 2"}, {"disabled"=>false, "hidden"=>false, "key"=>"4922bfbf-291a-047f-2273-6512cc0eb959", "type"=>"TextField", "data_name"=>"city", "required"=>false, "label"=>"City"}, {"disabled"=>false, "hidden"=>false, "key"=>"6d14d444-f5bc-6231-ac66-bb1e7d613f6a", "type"=>"TextField", "data_name"=>"state_province", "required"=>false, "label"=>"State / Province"}, {"disabled"=>false, "hidden"=>false, "multiple"=>false, "allow_other"=>false, "key"=>"c2bd0c84-967d-fe53-081e-530ef8e4e7bb", "type"=>"ChoiceField", "choice_list_id"=>"4fbce262ab0a5d57f600016c", "default_value"=>"United States", "data_name"=>"country", "required"=>false, "label"=>"Country"}, {"disabled"=>false, "hidden"=>false, "key"=>"a5c58cd8-fa3e-dc03-be15-60c6b3e5954d", "type"=>"TextField", "data_name"=>"phone", "required"=>false, "label"=>"Phone"}, {"disabled"=>false, "hidden"=>false, "key"=>"8d80bb77-0ff1-98ae-ffc5-e55f4c67f539", "type"=>"TextField", "data_name"=>"fax", "required"=>false, "label"=>"Fax"}, {"disabled"=>false, "hidden"=>false, "key"=>"fd10014d-c685-a953-a39b-9e466f8af884", "type"=>"TextField", "data_name"=>"email", "required"=>false, "label"=>"Email"}, {"disabled"=>false, "hidden"=>false, "key"=>"e1ca2a47-89ca-91cd-fbcd-9a7c36486cf9", "type"=>"TextField", "description"=>"If building is a notable, named building.", "data_name"=>"building_name", "required"=>false, "label"=>"Building Name"}, {"disabled"=>false, "hidden"=>false, "key"=>"1992b6b7-bf2f-6878-eacb-6b84a80992dd", "type"=>"PhotoField", "data_name"=>"photos", "required"=>false, "label"=>"Photos"}]}}

connection code:

class MyApi

  URI = nil #base api uri
  attr :connection

  def initialize(opts = {})
    @uri = opts[:uri] || URI
    @key = opts[:key] || get_key(opts[:username], opts[:password])
    raise ConnectionError, 'no api key' unless @key

    @connection = Faraday.new(@uri) do |b|
      b.adapter Faraday.default_adapter
      b.request :json
      b.response :logger
      b.response :raise_error
      b.response :json, :content_type => "application/json"
    end
    @connection.headers['X-ApiToken'] = @key
  end
end

so if i do:

api = MyApi.new(key)
api.connection.put("forms/#{id}.json", params)

i receive the above error and backtrace. originally, I had the request setup as

b.request :url_encode
b.request :multipart
@mislav
Copy link
Contributor

mislav commented May 25, 2012

You need to put adapter last in the list of middleware.

@mislav mislav closed this as completed May 25, 2012
@ckhall
Copy link
Author

ckhall commented May 25, 2012

thanks mislav!

@KarlGl
Copy link

KarlGl commented Sep 22, 2015

For anyone else having this problem I simply had to call .to_json on the hash I was sending as the body.

@a0s
Copy link

a0s commented May 30, 2017

Why Faraday not call to_json/to_x_www_form_urlencoded automatic according request's content type?

@iMacTia
Copy link
Member

iMacTia commented May 30, 2017

@a0s it does, if you use one of the request middlewares.
If you manually set the Content-Type header to any value, then faraday won't touch your body.
See the below example to set AND encode the body:

connection = Faraday.new(@uri) do |b|
    b.request :json # This will set the "Content-Type" header to application/json and call .to_json on the body
    b.adapter Faraday.default_adapter
end

If you prefer to use x_www_form_urlencoded instead, simply swap b.request :json with b.request :url_encoded

@danajackson2
Copy link

@iMacTia I believe it's b.request :url_encoded

@iMacTia
Copy link
Member

iMacTia commented Aug 7, 2021

@danajackson2 you're very right! I've edited my comment above, thanks for pointing it out and sorry for the typo 🙏 !

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

6 participants