为什么在此cURL调用的请求正文中收到格式错误的JSON? [英] Why do I get a malformed JSON in request body in this cURL call?

查看:403
本文介绍了为什么在此cURL调用的请求正文中收到格式错误的JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用他们自己的文档中提供的示例来调用CloudFlare API v4.

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

这是示例代码

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":{}}'

在以下位置也可以找到 更新DNS记录

Which can also be found at Update DNS Records

使用Windows cmd.exe运行此命令,我需要先使其成为单行,因此我删除了"\"并重新格式化(两次)以确保在此过程中没有任何改动.

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":{}}'

当我在cmd中运行此单行代码时,它可以工作,但是我在请求正文中收到格式错误的JSON,但是,目视检查,在notepad ++上进行格式化以及通过JSON验证程序运行都是肯定的,此JSON(复制自CloudFlare文档)没有格式错误.

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.

错误消息

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

搜索此错误消息或错误代码不会给我任何帮助,并且该命令也可以在运行我老板的Linux的PC上使用.

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

有人可以告诉我这是否是一个已知的错误,或者JSON是否真的格式错误或有其他想法吗?

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

谢谢

推荐答案

弗兰克·洛厄尔,谢谢你的提问!

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.

例如,对于清除所有内容 --data值将为:

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

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

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

对于Windows:

  1. 用双引号替换单引号:' --> "
  2. 用反斜杠转义双引号:" --> \"
  1. Replace the single quotes with double quotes: ' --> "
  2. Escape the double quotes with a backslash: " --> \"

这篇关于为什么在此cURL调用的请求正文中收到格式错误的JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆