How to send a POST request using HTTPie?

亡梦爱人 提交于 2019-12-18 09:34:36

问题


I have a basic silex application, and I try to test it using HTTPie. Yet when posting using:

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

The data, that I get from the Request object via:

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

will always be empty. What is the problem here?


回答1:


It was an httpie usage problem as the form flag was necessary, as silex requires the parameters to be form-encoded, yet the default of HTTPie is to pass a JSON object.

$ 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"
}



回答2:


Just to clarify what kOpernikus said, when you are making a POST request using httpie, use the following syntax:

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


来源:https://stackoverflow.com/questions/33128993/how-to-send-a-post-request-using-httpie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!