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

查看:29
本文介绍了为什么在此 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 记录

使用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.

这是同一行代码:

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":"请求正文中的 JSON 格式错误"}],"messages":[],";结果:空}

{"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.

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

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

推荐答案

我在博文中找到了答案:"期望在请求正文中找到有效的 JSON...";适用于 Windows 的 curl.

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:

For Windows:

  1. 用双引号代替单引号:' --> "
  2. 用反斜杠转义双引号:" --> "

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

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