Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send a POST request using HTTPie?

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?

like image 851
k0pernikus Avatar asked Oct 14 '15 15:10

k0pernikus


People also ask

How do I request a post from a URL?

POST request in itself means sending information in the body. I found a fairly simple way to do this. Use Postman by Google, which allows you to specify the content-type (a header field) as application/json and then provide name-value pairs as parameters. Just use your URL in the place of theirs.

What is HTTPie used for?

HTTPie (pronounced aitch-tee-tee-pie) is a command-line HTTP client. Its goal is to make CLI interaction with web services as human-friendly as possible. HTTPie is designed for testing, debugging, and generally interacting with APIs & HTTP servers. It supports both http and https; displays it in color by default.

How do you send data in a POST request?

One possible way to send a POST request over a socket to Media Server is using the cURL command-line tool. The data that you send in a POST request must adhere to specific formatting requirements. You can send only the following content types in a POST request to Media Server: application/x-www-form-urlencoded.


Video Answer


2 Answers

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"
}
like image 88
k0pernikus Avatar answered Oct 14 '22 06:10

k0pernikus


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!"

Alternatively, since forms are for post requests you can leave out post and also abbreviate --form to -f like so:

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

EDIT (thanks to Aerials)

To pass csrf token as header in the post request do:

http --form POST http://localhost:8000/login/ username=user password=pass X-CSRFToken:assQ$%auxASDLSIAJSd
like image 36
GilbertS Avatar answered Oct 14 '22 06:10

GilbertS