nolramlinux nolramlinux - 2 months ago 64
C# Question

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

I'm working with MailChimp API v3.0 and I need to create a new Segment, based on the MailChimp API documentation: http://developer.mailchimp.com/documentation/mailchimp/reference/lists/segments/

It's not working in my C# API Wrapper and I´m using CURL (suggested in the documentation) for testing purposes, specifically CURL for Windows: https://curl.haxx.se/ ...here is my command:

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


and the response message is:

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

Answer

Using Curl in windows cmd, the response only said:

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

But alternatively I used Git Bash( included by default when you install Git for Windows https://git-for-windows.github.io/ ) and the response was different and very useful:

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

Based on this message I added the "static_segment" parameter in JSON and everything works great when I adjusted the command option --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

Comments about the problem/solution:

Problem solved!

Comments