在 Windows cmd 中使用 CURL 请求调用 MailChimp API 3.0 时获取 400 错误请求

Get 400 Bad Request when calling MailChimp API 3.0 using CURL request in Windows cmd

我正在使用 MailChimp API v3.0,我需要创建一个新的 Segment,基于MailChimp API 文档:http://developer.mailchimp.com/documentation/mailchimp/reference/lists/segments/

它在我的 C# API 包装器中不起作用,我正在使用 CURL(文档中建议)进行测试,特别是 CURL 用于 Windowshttps://curl.haxx.se/ ...这是我的命令:

curl --request POST --url https://usXX.api.mailchimp.com/3.0/lists/my1i5t1d/segments --header "Authorization: apikey myap1k3y-usXX" --header 'Content-Type: application/json' --data '{"name":"myDynamicSegment"}' --include

响应消息是:

HTTP/1.1 400 Bad Request
Server: nginx
Content-Type: application/problem+json; charset=utf-8
Content-Length: 203
X-Request-Id: 1d001b85-0286-4339-ab37-3cb49c6f28f8
Link: <https://us13.api.mailchimp.com/schema/3.0/ProblemDetailDocument.json>; rel="describedBy"
Date: Thu, 14 Jul 2016 22:06:08 GMT
Connection: close
Set-Cookie: _AVESTA_ENVIRONMENT=prod; path=/

{"type":"http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/","title":"JSON Parse Error","status":400,"detail":"We encountered an unspecified JSON parsing error.","instance":""}

在windowscmd中使用Curl,响应只说:

{"type":"http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/","title":"JSON Parse Error","status":400,"detail":"We encountered an unspecified JSON parsing error.","instance":""}

但是我也使用了 Git Bash(当您为 Windows https://git-for-windows.github.io/ 安装 Git 时默认包含)并且响应是不同的并且非常有用:

{"type":"http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/","title":"Invalid Resource","status":400,"detail":"Must provide either an \"options\" field or a \"static_segment\" field","instance":""}

基于此消息,我在 JSON 中添加了 "static_segment" 参数,当我调整命令选项 --data:

时一切正常
curl --request POST --url https://usXX.api.mailchimp.com/3.0/lists/my1i5t1d/segments --header "Authorization: apikey myap1k3y-usXX" --header 'Content-Type: application/json' --data '{"name":"myDynamicSegment", "static_segment" : ["test1@domain.com", "test1@domain.com"]}' --include

关于 problem/solution 的评论:

问题已解决!