We all started somewhere! Share your experience on the Get Advice: Let's Get Started Sweepstakes thread and be entered to win a $100 credit on your Constant Contact account.

Error 400 C# Dictionary encoded json requirements for add contact


Error 400 C# Dictionary encoded json requirements for add contact

Trying to correctly format the req;uired parameters for adding a new contact but can't seem to get it right.   I start with a dictionary and  am trying to add the lists and email_addresses as arrays  before encoding and sending the request.  My code looks like this:


List<string> addies = new List<string>();
addies.Add("{ 'email_address' : 'test@gmail.com' }");


Dictionary<string, string[]> info = new Dictionary<string, string[]>();
info.Add("lists", new string[]{"'id' : '1475239787' "});
info.Add("email_addresses", new string[]{"'email_address' : 'test@gmail.com'"});


request.SetHeader("Content-Type", "application/json; charset=UTF-8");
request.AddHeader("Accept", "application/json");

request.RawData = System.Text.Encoding.UTF8.GetBytes(BestHTTP.JSON.Json.Encode(info));


but the server returns 400.




I expect that the cause of your error is because the JSON encoder is simply encoding your JSON objects as strings, so the API is not able to interpret them properly. I would suggest looking at the output of BestHTTP.JSON.Json.Encode(info)  and comparing it to the example JSON for the endpoint you're working with. Speaking of which, what is the API endpoint that you are making a request to with this call? In order to verify that you have the right JSON, it would help to have some clarity on this! (Since we have both individual and bulk add contacts, it's not clear which this is)



Elijah G.
API Support Engineer

Tracing the Debug.Log returns

{"lists":"[{'id' : '1475239787' }]", "email_addresses":"[{'email_address' : 'test@gmail.com'}]"}

I'm trying to add an individual contact via the

POST Add a Contact to the collection    /contacts
when I use 
to test I put in:
"lists": [
"id": "1475239787"
"email_addresses": [
"email_address": "username2@example.com"
and it works correctly giving me 200 code and the user showing up in the dashboard.
Thanks for the help too.

Looking at the details here the culprit is definitely the JSON serializer you're using. In the sample from your debug log, I've highlighted the offending characters that are converting the arrays of lists/emails into strings.


{"lists":"[{'id' : '1475239787' }]", "email_addresses":"[{'email_address' : 'test@gmail.com'}]"}



My suggestion would be to switch to a different serializer such as Json.NET


Please let me know if there's anything more that I can do to help!



Elijah G.
API Support Engineer
Developer Portal

View API documentation, code samples, get your API key.

Visit Page