109

I may be way off base, but I've been trying to run the curl post command in this recess PHP framework tutorial all afternoon. What I don't understand is how PHP is supposed to interpret my POST, it always comes up as an empty array.

curl -i -X POST -d '{"screencast":{"subject":"tools"}}'  \          http://localhost:3570/index.php/trainingServer/screencast.json

(The slash in there is just to make me not look like an idiot, but I executed this from Windows using PHP 5.2, I also tried it on a Linux server, the same version with Linux curl)

There must be something I'm missing because it seems pretty straightforward, the post just isn't being interpreted right, if it was, everything would work great.

This is what I got back:

HTTP/1.1 409 Conflict
Date: Fri, 01 May 2009 22:03:00 GMT
Server: Apache/2.2.8 (Win32) PHP/5.2.6
X-Powered-By: PHP/5.2.6
Transfer-Encoding: chunked
Content-Type: text/html; charset=iso-8859-1

{"screencast":{"id":null,"subject":null,"body":null,
         "dataUrl":null,"dataMedium":null,"createdOn":null,"author":null}}
7
  • Could you please copy-paste your .php file as well? Are you sure that the URL localhost:3570/index.php/trainingServer/screencast.json runs your script? It doesn't look like a PHP URL.
    – pts
    Commented May 1, 2009 at 22:11
  • @pts; Peter is using an MVC framework of some kind, take a look at index.php in that URL. Commented May 1, 2009 at 23:59
  • @pts I'm using Delphi for PHP (hence the :3570) and the Recess MVC framework w/out .htaccess file, hence the index.php/ in the url. Commented May 4, 2009 at 13:22
  • 7
    Don’t forget to send it as application/json.
    – Gumbo
    Commented Aug 16, 2009 at 11:30
  • 1
    Its not necessary to escape the double quotes when embedding them in simple ones.
    – user435996
    Commented Jan 20, 2011 at 11:51

5 Answers 5

120

Normally the parameter -d is interpreted as form-encoded. You need the -H parameter:

curl -v -H "Content-Type: application/json" -X POST -d '{"screencast":{"subject":"tools"}}' \
http://localhost:3570/index.php/trainingServer/screencast.json
107

Jordans analysis of why the $_POST-array isn't populated is correct. However, you can use

$data = file_get_contents("php://input");

to just retrieve the http body and handle it yourself. See PHP input/output streams.

From a protocol perspective this is actually more correct, since you're not really processing http multipart form data anyway. Also, use application/json as content-type when posting your request.

2
  • 7
    Doing json_decode(file_get_contents("php://input"), true) worked. Thanks Commented May 4, 2009 at 14:25
  • FANTASTIC. I was having a lot of trouble with Zend thinking it was stripping the post.. A simple extraction which worked for me. Also Peter Turners addition of json_decode() providing a \stdClass object. I used it for Garmin API ping responses
    – JI-Web
    Commented Mar 31, 2017 at 0:21
19

I believe you are getting an empty array because PHP is expecting the posted data to be in a Querystring format (key=value&key1=value1).

Try changing your curl request to:

curl -i -X POST -d 'json={"screencast":{"subject":"tools"}}'  \
      http://localhost:3570/index.php/trainingServer/screencast.json

and see if that helps any.

1
  • Yes, the -H "Content-Type: application/json" did not work for me.
    – Jay Brunet
    Commented Dec 7, 2017 at 4:14
14

You need to set a few extra flags so that curl sends the data as JSON.

command

$ curl -H "Content-Type: application/json" \
       -X POST \
       -d '{"JSON": "HERE"}' \
       http://localhost:3000/api/url

flags

  • -H: custom header, next argument is expected to be header
  • -X: custom HTTP verb, next argument is expected to be verb
  • -d: sends the next argument as data in an HTTP POST request

resources

1

You should escape the quotes like this:

curl -i -X POST -d '{\"screencast\":{\"subject\":\"tools\"}}'  \
  http://localhost:3570/index.php/trainingServer/screencast.json
2
  • On Windows this actually worked for me, but using double quotes to enclose the whole json string.
    – meatvest
    Commented Mar 14, 2012 at 15:01
  • on windows you have to escape double quotes as Josef suggested above. He is right.
    – Bobo
    Commented May 9, 2012 at 19:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.