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

半世苍凉 提交于 2019-12-06 21:29:54

问题


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


回答1:


Frank.Lowell, thank you for question!

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: " --> \"


来源:https://stackoverflow.com/questions/37806096/why-do-i-get-a-malformed-json-in-request-body-in-this-curl-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!