如何使用 HTTPie 发送 POST 请求?

How to send a POST request using HTTPie?

我有一个基本的 silex 应用程序,我尝试使用 HTTPie 对其进行测试。然而发帖时使用:

http POST http://localhost:1337 data="hello world"

data,我通过以下方式从 Request 对象获得:

$data = $request->request->get('data');

永远是空的。这里有什么问题?

这是一个 httpie 使用问题,因为 form 标志是必需的,因为 silex 需要对参数进行形式编码,但 HTTPie 的默认设置是传递一个 JSON 对象。

$ http --form POST http://localhost:1337 data="hello world"

HTTP/1.1 200 OK
Cache-Control: no-cache
Connection: close
Content-Type: application/json
Date: Wed, 14 Oct 2015 15:04:09 GMT
Host: localhost:1337
X-Powered-By: PHP/5.5.9-1ubuntu4.13

{
    "message": "hello world"
}

只是为了澄清 kOpernikus 所说的内容,当您使用 httpie 发出 POST 请求时,请使用以下语法:

   http --form post :3000/register username="gilbert" password="Whosebug!"

或者,由于表单用于 post 请求,您可以省略 post 并将 --form 缩写为 -f,如下所示:

   http -f :3000/register username=gilbert password=Whosebug!

编辑(感谢天线)

要在 post 请求中将 csrf 令牌作为 header 传递:

http --form POST http://localhost:8000/login/ username=user password=pass X-CSRFToken:assQ$%auxASDLSIAJSd