StatusCode: 400 and Bad Request with C# when call createOrUpdate Contact


#1

Hey guys, I’m trying to use the contact sync with C# but I’m receiving that error. I’m not sure if can be something with my json or with the header.

Error:
{StatusCode: 400, ReasonPhrase: ‘Bad Request’, Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Access-Control-Allow-Credentials: false
Vary: Accept-Encoding
Vary: Accept-Encoding
Connection: close
Date: Thu, 21 Jul 2016 11:28:09 GMT
Content-Length: 174
Content-Type: application/json; charset=UTF-8
}}

This is my Json
{“Properties”:[{“Property”:“firstname”,“Value”:“tiago api”},{“Property”:“lastname”,“Value”:“maximo api”}]}

My code:

        using (HttpClient httpClient = new HttpClient())
         {
            httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json; charset=utf-8");

            SyncContactObject p = new SyncContactObject();
            p.Properties.Add(new PropertyObject("firstname", "tiago api"));
           
            string postBody = JsonConvert.SerializeObject(p);

            var response = httpClient.PostAsync(uri, new StringContent(postBody, Encoding.UTF8, "application/json")).Result;

            return JsonConvert.DeserializeObject<string>(await response.Content.ReadAsStringAsync());
        }

#2

Hey @Tiago_Maximo,

I think it’s the capitalization in your JSON, try lower casing all the parameters.

In my own tests this payload was successful:

{"properties":[
  {"property":"firstname","value":"tiago api"},
  {"property":"lastname","value":"maximo api"}]
}

but if I capitalized any of the params I got this 400 error:

{
  status:"error",
  message:"Json node is missing child property",
  correlationId:"c082e9d2-5a03-479a-8af1-d3da4191c386",
  requestId:"f5cc1c40-711f-4a5f-b299-6206eb22d4ef"
}

Also, it might help to print the full error message, looks like you’re just getting a generic Bad Request response related to the 400.

Sebastian


#3

Thank you so much, that’s was my problem.

Just removed the capitalization and worked!!!


#4

Hi,

I am also getting same problem even i made everything lowercase. below is my request. kindly help me out.

string DATA = "{}"+ Environment.NewLine + " {"file":"IMproductSearch","params":{"pageStart": 0,"pageSize": 50,"listOfProducts":["PB0047"]}}";

SerializedRequest = JsonConvert.SerializeObject(DATA);

        string URL = "https://api-beta.ingrammicro.com:443/ea-api/v1/productsearchus";
        
        System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
        client.BaseAddress = new System.Uri(URL);
        byte[] cred = UTF8Encoding.UTF8.GetBytes("username:password");
        client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(cred));
        client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

        System.Net.Http.HttpContent content = new StringContent(JsonConvert.SerializeObject(DATA.ToLower()), UTF8Encoding.UTF8, "application/json");
        HttpResponseMessage messge = client.PostAsync(URL, content).Result;
        string description = string.Empty;
        if (messge.IsSuccessStatusCode)
        {
            string result = messge.Content.ReadAsStringAsync().Result;
            description = result;
        }