20

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?

k0pernikus
  • 60,309
  • 67
  • 216
  • 347

2 Answers2

25

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"
}
k0pernikus
  • 60,309
  • 67
  • 216
  • 347
6

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
GilbertS
  • 591
  • 8
  • 12
  • What if you have to pass a `csrf token` in the POST request along with username and password? – Aerials Feb 24 '20 at 19:10
  • Well it depends. If the csrf token is to be passed in the form and not the headers you can use: http -f :3000/profile username='harantula' password='itmo' csrftoken='akskadlsjd123$11d' – GilbertS Feb 24 '20 at 20:15
  • 1
    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` – Aerials Feb 24 '20 at 22:05