John
John

Reputation: 3050

curl POST json via Terminal to PHP, Syntax error

Attempt 1:

curl -X POST -H "Content-Type:application/json" -H "Accept:applica
tion/json" http://localhost/test.php -d '{"helloworld: "Hello"
}

Attempt 2:

curl -X POST -H "Content-Type: application/json" -d '{"username":"xyz","password":"xyz"}' http://localhost/test.php

Both are giving the output: "Syntax Error", I've tried Attempt 2 with double quotes around, etc, all giving me Syntax error.

Here is test.php

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

$data = json_decode($data);

echo json_last_error_msg();

Any solutions or easier ways to accomplish this? GET/POST requests to the PHP script.

Upvotes: 0

Views: 428

Answers (1)

Sabuj Hassan
Sabuj Hassan

Reputation: 39355

I believe you are doing this from Windows operating system. You have to use double quotes instead of single around the JSON parameters. And you have to escape the double quotes which are inside with back slash(\).

-d "{\"username\":\"xyz\",\"password\":\"xyz\"}"

Hope this will help you.

Upvotes: 1

Related Questions