Frank.Lowell Frank.Lowell - 5 months ago 33
JSON Question

Why do I get a malformed JSON in request body in this cURL call?

I have been trying to call the CloudFlare API v4, using an example provided in their own documentation.

This is the code of the example

curl -X PUT "https://api.cloudflare.com/client/v4/zones/023e105f4ecef8ad9ca31a8372d0c353/dns_records/372e67954025e0ba6aaa6d586b9e0b59" \ -H "X-Auth-Email: user@example.com" \ -H "X-Auth-Key: c2547eb745079dac9320b638f5e225cf483cc5cfdda41" \ -H "Content-Type: application/json" \ --data '{"id":"372e67954025e0ba6aaa6d586b9e0b59","type":"A","name":"example.com","content":"1.2.3.4","proxiable":true,"proxied":false,"ttl":120,"locked":false,"zone_id":"023e105f4ecef8ad9ca31a8372d0c353","zone_name":"example.com","created_on":"2014-01-01T05:20:00.12345Z","modified_on":"2014-01-01T05:20:00.12345Z","data":{}}'


Which can also be found at
Update DNS Records

Using Windows cmd.exe to run this command, i need to make it single line first, so i removed the "\" and reformatted it (twice) making sure I altered no part in the process.

This is the same code in one line :

curl -X PUT "https://api.cloudflare.com/client/v4/zones/023e105f4ecef8ad9ca31a8372d0c353/dns_records/372e67954025e0ba6aaa6d586b9e0b59" -H "X-Auth-Email: user@example.com" -H "X-Auth-Key: c2547eb745079dac9320b638f5e225cf483cc5cfdda41" -H "Content-Type: application/json" --data '{"id":"372e67954025e0ba6aaa6d586b9e0b59","type":"A","name":"example.com","content":"1.2.3.4","proxiable":true,"proxied":false,"ttl":120,"locked":false,"zone_id":"023e105f4ecef8ad9ca31a8372d0c353","zone_name":"example.com","created_on":"2014-01-01T05:20:00.12345Z","modified_on":"2014-01-01T05:20:00.12345Z","data":{}}'


When i run this single-liner in cmd, it works but i get a malformed JSON in request body, however, a visual check, formatting on notepad++ and a run through the JSON validator are all positive, this JSON (copied from the CloudFlare documentation) is not malformed.

Error Message

{"success":false,"errors":[{"code":6007,"message":"Malformed JSON in request body"}],"messages":[],"result":null}


Googling this error message or the error code gives me nothing and this same command works on a pc running linux of my boss.

Can someone tell me if this is a known bug, if the JSON really is malformed or if something else comes to mind ?

Thank you

Answer

Frank.Lowell, thank you for question!

I found the answer in blog post: "Expecting to find valid JSON in request body..." curl for Windows.

As example, for Purge everything --data value will be:

# On Linux
--data '{"purge_everything":true}'

# On Windows
--data "{\"purge_everything\":true}"

For Windows:

  1. Replace the single quotes with double quotes: ' --> "
  2. Escape the double quotes with a backslash: " --> \"