suhasa
suhasa

Reputation: 107

Sending Array as a body in HTTParty post request giving : undefined method `bytesize' for #<Array

I am trying to make a post request using httparty gem.

    response = HTTParty.post(App.base_uri + "/api?service=post&action=update&version=2",
    body: [{"some_big_json": "with many key value pair"}],
    headers: {
      'Content-Type' => 'application/json',
      'Accept' => 'application/json',
      'Cookie' => @@cookie
    })

The Body is an array. When i make a post request, I am getting this error:

/home/user/.rbenv/versions/2.5.0/lib/ruby/2.5.0/net/http/generic_request.rb:183:in send_request_with_body': undefined method bytesize' for #<Array:0x00005561f96833e0> (NoMethodError)

How to post a large array in body using httparty?

Update: In the postman i am able to send body as an array and i am getting successful(expected) response.

Upvotes: 1

Views: 1842

Answers (3)

Dan Wetherald
Dan Wetherald

Reputation: 1

Just convert the array to JSON before submitting.

options = @options.merge(
  body: [
    {
      name: 'John Doe',
    }
  ].to_json
)

Upvotes: 0

awsm sid
awsm sid

Reputation: 595

You can also try the RestClient

RestClient::Request.execute(method: :post,
                            url: App.base_uri + '/api',
                            payload: params,
                            timeout: 1000,
                            headers: headers)

Upvotes: 0

awsm sid
awsm sid

Reputation: 595

Try to pass the string rather than array.

response = HTTParty.post(App.base_uri + "/api?service=post&action=update&version=2",
    body: "[{'some_big_json': 'with many key value pair'}]",
    headers: {
      'Content-Type' => 'application/json',
      'Accept' => 'application/json',
      'Cookie' => @@cookie
    })

Upvotes: 2

Related Questions