Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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: [email protected]" \ -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: [email protected]" -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.

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

like image 442
Frank.Lowell Avatar asked Jun 14 '16 07:06

Frank.Lowell


People also ask

What causes malformed JSON?

Cause. The malformed JSON error means that the format of the API call is incorrect.

How do I get JSON with curl?

To get JSON with Curl, you need to make an HTTP GET request and provide the Accept: application/json request header. The application/json request header is passed to the server with the curl -H command-line option and tells the server that the client is expecting JSON in response.


1 Answers

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

For 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: " --> \"
like image 127
Veaceslav Doina Avatar answered Sep 16 '22 17:09

Veaceslav Doina