0

My command is: curl -X POST --user "admin:admin" https://website.com/wp-json/wp/v2/posts/ -H "Content-Type: application/json" -d {"title":"test","content":"Content","status":"draft"}

The response I get: {"code":"rest_invalid_json","message":"Invalid JSON body passed.","data":{"status":400,"json_error_code":4,"json_error_message":"Syntax error"}}

Thanks!

  • Does this do the trick? https://wordpress.org/support/topic/failed-to-decode-json-in-imported-data-error-code-4/ – thiebo Feb 07 '21 at 05:34
  • Do you need to use single quotes in the command, like this: `'{"title":"test","content":"Content","status":"draft"}'`? I checked the json itself using the validator that came up on [this page](https://duckduckgo.com/?q=json+lint&ia=answer) and the json itself was valid. – summea Feb 07 '21 at 05:40
  • From the curl docs, it looks like the data after a `-d` might need to have quotes of some sort as seen in the [POST (HTTP)](https://curl.se/docs/manual.html) section. In this case, since you are already using double quotes in the json itself, it might help to use single quotes around the outside of the json. – summea Feb 07 '21 at 05:48
  • I've tried using single and double quotes around the data, doesn't change the response I get. – D1EM4CH1N3 Feb 07 '21 at 07:33
  • @D1EM4CH1N3 Sorry to hear that the response has not changed on your side. I've updated my answer below to share what I found when I tried doing this type of request on my own WordPress site. I ended up needing to use single quotes around the json, but I also needed to use an _application password_ for the `--user` credentials part. – summea Feb 09 '21 at 03:56

2 Answers2

0

From my earlier comments, I have the feeling that this error is related to the -d possibly needing to have quotes around the data text.

So in your case, it might be worth trying to put single quotes around the -d data text because you are currently using double quotes in the json text.

Thus, your curl command might look like this:

curl -X POST --user "admin:admin" https://website.com/wp-json/wp/v2/posts/ -H "Content-Type: application/json" -d '{"title":"test","content":"Content","status":"draft"}'

This is based on how the curl docs appear to show that quotes should be used with -d related data text based on the example under the "POST (HTTP)" section of the curl documentation here. I think this is what I have done in the past, as well, but I don't have a specific personal example on me at the moment.


Update

For what it's worth, when I tried running this curl type of command on my own, I got the following error when I didn't use single quotes around the json text:

{"code":"rest_invalid_json","message":"Invalid JSON body passed.","data":{"status":400,"json_error_code":4,"json_error_message":"Syntax error"}}curl: (3) Port number ended with 'C' curl: (3) Port number ended with 'd'

When I ran the command with single quotes around the json data, like this: -d '{"title":"test","content":"Content","status":"draft"}', the command worked and a draft was added in WordPress. I also received back a json response that looks related to the new draft post.

Another thing to check might be the credentials used with the --user part. I ended up having to use an "Application Password" that I generated for a user in my WordPress account. This is different than my user's login password. If you haven't already tried using an "Application Password" in WordPress for this part, it might be worth trying to see if that fixes your issue. I found this when I:

  1. Logged into my WordPress website
  2. Clicked on the Users menu item
  3. Clicked on a user
  4. Scrolled down to the bottom of the user info page
  5. Clicked on the Add New Application Password button
summea
  • 7,390
  • 4
  • 32
  • 48
  • 1
    Thanks for the help, I tried the application passwords, that was one of issues I'm pretty sure. I thought I was going insane but the problem turned out to be a windows & curl thing. – D1EM4CH1N3 Feb 09 '21 at 04:43
  • @D1EM4CH1N3 Glad to hear that it's working now! – summea Feb 09 '21 at 04:48
0

Ok, this was a windows specific problem, I guess most people out there are running unix/linux clients so this doesn't apply to them. I found the solution here: https://stackoverflow.com/a/7173011/15161479

The issue is with curl on windows, the quotes need to be escaped. I also did a couple other things like installing the "Application Passwords" plugin.

This is what my command looks like now curl --user "user:application password" http://website.com/wp-json/wp/v2/posts/ -H "Content-Type: application/json" --data "{"""title""":"""test""","""content""":"""Content""","""status""":"""draft"""}"

Hope this helps some other people out there!