sendinblue / APIv3-csharp-library

SendinBlue's C# library for API v3
MIT License
58 stars 26 forks source link

AddContactToList deserialization error #31

Closed kccsf closed 3 years ago

kccsf commented 3 years ago

Upgraded sib_api_v3_sdk.dll from 1.0.0 to the current latest (3.1.0) which did not resolve the issue.

Error: sib_api_v3_sdk.Client.ApiException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[System.String]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path 'contacts.success.ids', line 1, position 30.'

Upgraded sib_api_v3_sdk.dll from 1.0.0 to the current latest (3.1.0) which did not resolve the issue.

Code to recreate (email address must not be in the list already):

var emailAddress = "john.doe@sendinblue.com";
var configuration = new Configuration();
configuration.AddApiKey("api-key", "<redacted>");

var contactsApiInstance = new ContactsApi(configuration);

var attributes = new Dictionary<string, string>
{
     { "FIRSTNAME", "John" },
     { "LASTNAME", "Doe" }
};

var createContact = new CreateContact(emailAddress, attributes, updateEnabled: true);
contactsApiInstance.CreateContact(createContact);
var emails = new List<string>() { emailAddress };
var addContactToList = new AddContactToList(emails);
contactsApiInstance.AddContactToList(2, addContactToList);

Returned json: {"contacts":{"success":{"ids":[],"emails":["john.doe@sendinblue.com"]},"failure":[]}}

amitsendinblue commented 3 years ago

Hi @kccsf, It looks like you are calling two API routes in above example contactsApiInstance.CreateContact and contactsApiInstance.AddContactToList. Please use the examples mentioned in the reference document.

Create a contact

using sib_api_v3_sdk.Api;
using sib_api_v3_sdk.Client;
using sib_api_v3_sdk.Model;
using System;
using System.Diagnostics;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
namespace Sendinblue
{
    class Program
    {
        static void Main(string[] args)
        {
            Configuration.Default.ApiKey.Add("api-key", "YOUR API KEY");
            var apiInstance = new ContactsApi();
            string email = "example@example.com";
            JObject attributes = new JObject();
            attributes.Add("FIRSTNAME", "John");
            attributes.Add("LASTNAME", "Doe");
            attributes.Add("SMS", "91XXXXXXXXXX");
            List<long?> listIds = new List<long?>();
            listIds.Add(2);
            bool emailBlacklisted = false;
            bool smsBlacklisted = false;
            bool updateEnabled = false;
            List<string> smtpBlacklistSender = new List<string>();
            smtpBlacklistSender.Add("example@example.com");
            try
            {
                var createContact = new CreateContact(email, attributes, emailBlacklisted, smsBlacklisted, listIds, updateEnabled, smtpBlacklistSender);
                CreateUpdateContactModel result = apiInstance.CreateContact(createContact);
                Debug.WriteLine(result.ToJson());
                Console.WriteLine(result.ToJson());
                Console.ReadLine();
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
                Console.WriteLine(e.Message);
                Console.ReadLine();
            }
        }
    }
}

Add existing contacts to a list

using sib_api_v3_sdk.Api;
using sib_api_v3_sdk.Client;
using sib_api_v3_sdk.Model;
using System;
using System.Diagnostics;
using System.Collections.Generic;
namespace Sendinblue
{
    class Program
    {
        static void Main(string[] args)
        {
            Configuration.Default.ApiKey.Add("api-key", "YOUR API KEY");
            var apiInstance = new ContactsApi();
            long? listId = 2;
            List<string> emails = new List<string>();
            emails.Add("example@example.com");
            try
            {
                var contactEmails = new AddContactToList(emails);
                PostContactInfo result = apiInstance.AddContactToList(listId, contactEmails);
                Debug.WriteLine(result.ToJson());
                Console.WriteLine(result.ToJson());
                Console.ReadLine();
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
                Console.WriteLine(e.Message);
                Console.ReadLine();
            }
        }
    }
}

Thanks

kccsf commented 3 years ago

Hi @amitsendinblue

Thank you for the response. However, the test code posted above and live implementation are working again today. Perhaps someone reverted an api change...: Today's response: {"contacts":{"success":["john.doe@sendinblue.com"],"failure":[]}}

Yesterday's response: {"contacts":{"success":{"ids":[],"emails":["john.doe@sendinblue.com"]},"failure":[]}}