2

Just been tinkering with Sinatra and trying to get a bit of a restful web service going. The error I'm getting at the moment is very specific though.

Take this example post method

post '/postMan/:someParam' do
    #Edited here. This code can be anything. 411 is still the response
    puts params[:someParam]

end

Seems simple enough. Take a param, make an object out of it, then go store it in whatever way the objects save method defines.

Heres what I use to post the data using Curl

$curl -I -X POST http://127.0.0.1/postman/123456

The only problem is, I'm getting 411 back and have no idea why. To the best of my knowledge, 411 is length required. Here is the trace

HTTP/1.1 411 Length Required 
Content-Type: text/html; charset=ISO-8859-1
Server: WEBrick/1.3.1 (Ruby/1.9.2/2011-07-09)
Date: Fri, 02 Mar 2012 22:27:09 GMT
Content-Length: 303
Connection: close

I 'Cannot' change the curl message in any way. So might anyone have a way to set the content length to be ignored in sinatra? Or some fix which doesn't involve changing the curl request?


For the record, it doesn't matter whether I use the parameters in the Post method or not. I could have some crazy code inside it, it will still throw the same error

OVERTONE
  • 11,797
  • 20
  • 71
  • 87
  • 1
    When you actually post data (e.g. with `-d key=val`) do you get the same result? – nategood Mar 02 '12 at 22:47
  • @nategood I'm not familiar with curl too much. If this is correct 'curl -d key=val http://127.0.0.1:4567/postman/123456' Then the response is the default sinatra 404 (AKA sinatra doesn't know this ditty) – OVERTONE Mar 02 '12 at 22:52
  • Try `curl -I -X POST -d "key=val" http://127.0.0.1/postman/123456` – nategood Mar 02 '12 at 23:01
  • @nategood response is ""Warning: You can only select one HTTP request!"", sorry but thats probably something I've done wrong. – OVERTONE Mar 02 '12 at 23:03

4 Answers4

5

As others said above, WEBrick wrongly requires POST requests to have a Content-Length header. I just pass an empty body, because it's less typing than passing in the header:

curl -X POST -d '' http://webrickwhyyounotakeemptyposts.com/
Asfand Qazi
  • 6,586
  • 4
  • 32
  • 34
1

WEBrick erroneously requires POST requests to include the Content-Length header.

curl -H 'Content-Length: 0' -X POST http://example.com

Standardly, however, POST requests don't require a body and therefore don't require the Content-Length header.

Community
  • 1
  • 1
ma11hew28
  • 121,420
  • 116
  • 450
  • 651
1

Ah. Try it without -I. It's probably sending a HEAD request and as such, not sending what you expect. Use -v if you want to show the headers.

curl -v -X POST http://127.0.0.1/postman/123456

curl -v -X POST -d "key=val" http://127.0.0.1/postman/123456
nategood
  • 11,807
  • 4
  • 36
  • 44
  • Sadly this didn't work. The output of the request is here. It's still giving a 411. http://pastebin.com/t9rumPAu Bare in mind, that in the final version. I cannot change the curl command. – OVERTONE Mar 02 '12 at 23:15
1

Are you sure you're on port 80 for your app? When I run:

ruby -r sinatra -e "post('/postMan/:someParam'){puts params[:someParam]}"

and curl it:

curl -I -X POST http://127.0.0.1:4567/postMan/123456                                                                            
HTTP/1.1 200 OK
X-Frame-Options: sameorigin
X-XSS-Protection: 1; mode=block
Content-Type: text/html;charset=utf-8
Content-Length: 0
Connection: keep-alive
Server: thin 1.3.1 codename Triple Espresso

it's ok. Had to change the URL to postManthough, your example threw a 404because you had postman.

The output was also as expected:

== Sinatra/1.3.2 has taken the stage on 4567 for development with backup from Thin
>> Thin web server (v1.3.1 codename Triple Espresso)
>> Maximum connections set to 1024
>> Listening on 0.0.0.0:4567, CTRL+C to stop
123456
three
  • 8,262
  • 3
  • 35
  • 39
  • I think its running on a default of port :4567. I've tried your way but perhaps it has got something to do with my sinatra setup. One thing, I'm using webrick, not thin – OVERTONE Mar 03 '12 at 14:40
  • BY GOD YOU'VE DONE IT!!!! I switched from webrick to thin. All of a sudden, it starts working. maybe webrick doesn't account for content length. Thanks so much! – OVERTONE Mar 03 '12 at 14:48
  • thin is default in newer sinatra versions. I'd never think about using webrick. It's like the slowest player on the field. Use thin or puma! – three Mar 03 '12 at 16:31