(400) Bad Request


#1

Hi,

I’ve been trying to add a contact to HubSpot via my test portal from some .NET code on my development server using one of the two following APIs (where xxx is the API key I was assigned last week):

http://api.hubapi.com/contacts/v1/contact/createOrUpdate/email/asmith@goobers.com/?hapikey=xxx
http://api.hubapi.com/contacts/v1/contact/?hapikey=xxx

Additionally, the following attributes have been assigned to my webRequest:

webRequest.Method = “POST”;
webRequest.ContentType = “application/json”;

I’ve tried passing the JSON to HubSpot as a serialized string like so (prior to serialization):

“{“properties”:[{“property”:“email”,“value”:“asmith@goobers.com”},{“property”:“firstname”,“value”:“Adam”},{“property”:“lastname”,“value”:“Smith”}]}”;

However, testing has resulted in the following response: The remote server returned an error: (400) Bad Request.

Thinking that perhaps the above string was JSON-format challenged, I tested it at JSONLint.com where it came back as valid JSON. I therefore am finding myself at a loss for where I’m going astray and would be most appreciative of any assistance you could provide.

Thanks,
JJ


#2

Hi JJ,
Nothing looks immediately wrong in your JSON to me, but there are several things I can think of that might be going on. Most likely this is an encoding/serialization issue of some sort.

I’m not super familiar with .NET, but if you can log the exact request you are sending and the full response you are getting back from HubSpot, I can try to give you a better idea of what the error is.

Thanks,
Dillon


#3

Hi Dillon,

Thanks for the reply. Upon stepping through the code with some fresh eyes and making some minor adjustments, I was able to garner a response from HubSpot and get my contact added. Seems the third-party online example I found for making a JSON call in .NET, while directionally accurate, was an incomplete solution.

Thanks,
JJ


#4

Glad you got it sorted! Let us know if you have any other questions.

//Dillon


#5

Please can you share you solution? I’m having a similar problem in C# updating a contact. I’ve tried these endpoints and neither works

https://api.hubapi.com/contacts/v1/contact/createOrUpdate/email/contact-email/?hapikey=api-key
and
https://api.hubapi.com/contacts/v1/contact/vid/contact-id/profile?hapikey=api-key

and here is my JSOM sample data

“{‘properties’: [{‘property’:‘email’,‘value’:‘a@b.com’},{‘property’: ‘firstname’,‘value’: ‘Carmen’},{‘property’: ‘lastname’,‘value’: ‘San Diego’},{‘property’: ‘lifecyclestage’,‘value’: ‘customer’}]}”

I keep getting 400 Bad request.

Thanks
L


#6

Never mind I found the solution to my problem. I changed single quotes( ’ ) to (") in the JSON string and it worked fine.


#7

Well, now I would really much appreciate a C# example that is actually working :slight_smile:

Kind regards,
Henrik


#8

Hi Henrik,

Here’s the code I used to get the desired result.

Good Luck,
JJ

==========================================================================

public static string UploadHubSpotContact(DataTable dt)
{
  var contactEmail = dt.Rows[0].Field<string>("Email");
  var hubSpotApiKey = ConfigurationManager.AppSettings["HubSpotApiKey"];
  var hubSpotRestApi = string.Concat("http://api.hubapi.com/contacts/v1/contact/createOrUpdate/email/", contactEmail, "/?hapikey=", hubSpotApiKey);
  var responseText = "";
  var webRequest = WebRequest.Create(new Uri(hubSpotRestApi)) as HttpWebRequest;

  if (webRequest != null)
  {
    webRequest.Method = "POST";
    webRequest.ContentType = "application/json";
    
    var json = string.Concat("{\"properties\":[",
      "{\"property\":\"email\",\"value\":\"", contactEmail, "\"},",
      "{\"property\":\"firstname\",\"value\":\"", dt.Rows[0].Field<string>("FirstName"), "\"},",
      "{\"property\":\"lastname\",\"value\":\"", dt.Rows[0].Field<string>("LastName"), "\"},",
      "{\"property\":\"company\",\"value\":\"", dt.Rows[0].Field<string>("Company"), "\"},",
      "{\"property\":\"jobtitle\",\"value\":\"", dt.Rows[0].Field<string>("Title"), "\"},",
      "{\"property\":\"phone\",\"value\":\"", dt.Rows[0].Field<string>("Phone"), "\"},",
      "{\"property\":\"lifecyclestage\",\"value\":\"lead\"}]}");

    using (var requestWriter = new StreamWriter(webRequest.GetRequestStream()))
    {
      requestWriter.Write(json);
    }

    var response = webRequest.GetResponse();
    using (var reader = new StreamReader(response.GetResponseStream()))
    {
      responseText = reader.ReadToEnd();
    }
  }

  return responseText;
}

#9

Thanx, that worked perfectly!

Now I’m having trouble getting the contact via the api:
https://api.hubapi.com/contacts/v1/contact/email/qwerty@qwerty.dk/profile?hapikey=MyKey

In Fiddler and a browser it works perfect and I’m getting a perfectly formatted JSon in return.- But from my C# code I get a 404 error even though it’s the exact same url I’m sending via a WebRequest (GET).

Any help would be appreciated :slight_smile:


#10

Hi Henrik,

I was able to garner a result using a simplified version of the code I posted earlier, substituting the APIs and changing the method to a GET. Could you please post your code for review?

Thanks,
JJ


#11

I am getting a similar error with some contact properties I am trying to update. The properties that fail appear to have a list of values in HS and the value I am sending in not in the list.
Is there a way to disable the value checking via the API?