Sinatra: Problem with POST request

Created on 6 Jun 2016  路  4Comments  路  Source: sinatra/sinatra

I am working on an API where I need to send a pretty large POST JSON body. It's also pretty complicated JSON structure. However, when the body reaches a certain size, I get a RangeError exception in utils.rb:135:in normalize_params. In fact, there seems to be 100 calls to normailze_params. I have attached the stack trace.

I though it had only to do with size, but I tried with a simple json file of similar size, and it worked. The problem is the same for thin and WEBrick servers.

It is definitely a sinatra/server issue since parsing the same file in pure ruby works fine.

Any help in this matter is appreciated.

Regards

Mats

rangeerror.txt

Most helpful comment

I tried to reproduce the error with the sample JSON files and I found that this error is caused due to a missing content type specification in the request. Since the content type is missing, Rack is considering the entire string as a query parameter and is trying to convert it recursively. For this, there's a limit of 100 recursive calls after which Rack will preemptively raise a RangeError.

To avoid this error, see if you can specify the content type of the request as json. For instance:

wget http://localhost:4567 --post-file=not_working.json --header='Content-Type: application/json'

works fine with the not working input you've provided. In this case, Rack won't trigger the recursive params call.

All 4 comments

I have attached two obfuscated json files, one which provokes the error and one which does not. They only differ in one array item. The sinatra code is:

require 'sinatra'
require 'multi_json'

post '/' do

  body = MultiJson.load(request.body.read)

  response = body

  [ 201, response.to_json ]
end

obfusc_not_working.json.txt
obfusc_working.json.txt

I tried to reproduce the error with the sample JSON files and I found that this error is caused due to a missing content type specification in the request. Since the content type is missing, Rack is considering the entire string as a query parameter and is trying to convert it recursively. For this, there's a limit of 100 recursive calls after which Rack will preemptively raise a RangeError.

To avoid this error, see if you can specify the content type of the request as json. For instance:

wget http://localhost:4567 --post-file=not_working.json --header='Content-Type: application/json'

works fine with the not working input you've provided. In this case, Rack won't trigger the recursive params call.

Thank you so much! I should have known I goofed up myself.

No problem! That issue is a bit hard to figure out since the error is not localized in anyway (happens in Rack). I'm unable to imagine a note somewhere in the docs about this problem.

Was this page helpful?
0 / 5 - 0 ratings