不能使用Curl接受Sinatra的帖子

时间:2012-03-02 22:38:16

标签: ruby rest post curl sinatra

刚刚修补Sinatra并试图获得一些宁静的网络服务。 我现在得到的错误是非常具体的。

以示例post方法

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

end

看起来很简单。拿一个参数,从中创建一个对象,然后以对象保存方法定义的任何方式存储它。

继承人使用Curl发布数据

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

唯一的问题是,我回到了411并且不知道为什么。 据我所知,411是需要的长度。这是跟踪

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

我'不能'以任何方式改变卷曲消息。那么有没有人可以设置在sinatra中忽略的内容长度?或者一些不涉及更改卷曲请求的修复?


对于记录,我是否使用Post方法中的参数并不重要。我内心可能有一些疯狂的代码,它仍然会抛出相同的错误

4 个答案:

答案 0 :(得分:2)

正如其他人所说,WEBrick错误地要求POST请求具有Content-Length标头。我只是传递一个空的身体,因为它输入的次数少于传入标题:

curl -X POST -d '' http://webrickwhyyounotakeemptyposts.com/

答案 1 :(得分:1)

阿。不用-I试试吧。它可能会发送HEAD请求,因此不会发送您期望的内容。如果要显示标题,请使用-v

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

答案 2 :(得分:1)

WEBrick错误地要求POST请求包含Content-Length标题。

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

但标准地说,POST requests don't require a body因此不需要Content-Length标题。

答案 3 :(得分:0)

你确定你的应用程序在端口80上吗? 我跑的时候:

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

并卷曲它:

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

没关系。但是,您必须将网址更改为postMan,因为您拥有404,因此您的示例投放了postman

输出也符合预期:

== 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