The Community is hosting an End of Summer sweepstakes! Participants must complete tasks to earn tickets that will enter them with a chance to win a free year of Constant Contact and other great prizes!*
*No Purchase Necessary. For Official Rules, visit here. Constant Contact’s End of Summer 2020 Sweepstakes ends on October, 20, 2020 at 11:50 PM EST.

Error 400 C# Dictionary encoded json requirements for add contact

Highlighted
Visitor

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));
request.Send();

 

but the server returns 400.

3 REPLIES 3
Highlighted
Honored Contributor

Re: Error 400 C# Dictionary encoded json requirements for add contact

Hello,

 

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)

 

Sincerely,

Elijah G.
API Support Engineer
Highlighted
Visitor

Re: Error 400 C# Dictionary encoded json requirements for add contact

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.
 
Highlighted
Honored Contributor

Re: Error 400 C# Dictionary encoded json requirements for add contact

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!

 

Sincerely,

Elijah G.
API Support Engineer
Developer Portal

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

Visit Page

Constant Contact 2020 End of Summer Community Sweepstakes!

The Constant Contact User Community is hosting a sweepstakes. The more you participate, the more chances you have to win! Read on to learn more...

Read More
Featured