APIs & Integrations

gamebenchjake
Participant

Create Contact: 400 Bad Request

Hi everybody,

I’m currently implementing a function within my company’s web dashboard to send all contacts that have verified their email address and logged in, over to Hubspot.

We’re using MEAN stack, and I’m making the call from Express to the Hubspot API using the API Key auth method.

We have a variety of custom fields that I’m looking to fill, and all of these are setup in Hubspot already. I’ve got the connection working with the example JSON displayed in the API docs, but when I try and use our custom fields, I’m getting a 400 Bad Request error.

Please see the code I’m using below:

  function getMidnight(t) {
    t = t.setHours(0,0,0,0);
    return new Date(t).getTime();
  }

var sendToCRM = function(req, res, form) {

  console.log("Sending contact to Hubspot")
  var user = form;

  User.findOne({email: user.email}, function(erro,resul) {
    if (resul) {
      if (typeof resul.ContactId != "undefined") {
        console.log("Already in CRM")
      } else {
        var path = "/contacts/v1/contact/";

        var date = getMidnight(new Date());
        var fdate = getMidnight(new Date(resul.firstLogin));

        var email = resul.email;

    /* THIS WORKS */
    /* postBody = {
        "properties": [
            {
                "property": "email",
                "value": "testingapis@hubspot.com"
            },
            {
                "property": "firstname",
                "value": "Adrian"
            },
            {
                "property": "lastname",
                "value": "Mott"
            },
            {
                "property": "website",
                "value": "http://hubspot.com"
            },
            {
                "property": "company",
                "value": "HubSpot"
            },
            {
                "property": "phone",
                "value": "555-122-2323"
            },
            {
                "property": "address",
                "value": "25 First Street"
            },
            {
                "property": "city",
                "value": "Cambridge"
            },
            {
                "property": "state",
                "value": "MA"
            },
            {
                "property": "zip",
                "value": "02139"
            }
        ]
    };
*/

    /* THIS DOESN'T WORK */
        var postBody = { "properties": [
          {
            "property": "email",
            "value": email
          },
          {
            "property": "firstname",
            "value": resul.fname
          },
          {
            "property": "lastname",
            "value": resul.lname
          },
          {
            "property": "gb_first_login",
            "value": fdate
          },
          {
            "property": "gb_last_login",
            "value": date
          },
          {
            "property": "gb_login_counter",
            "value": resul.loginCounter
          },
          {
            "property": "verification_email_counter",
            "value": resul.verEmailCounter || 1
          },
          {
            "property": "gb_referral",
            "value": resul.ref
          },
          {
            "property": "ipaddress",
            "value": (req.headers['x-forwarded-for'] || req.connection.remoteAddress || req.socket.remoteAddress || req.connection.socket.remoteAddress)
          }
        ]
      };
        
        api.CRMRequest(path, postBody, function(statusCode, result)
        {
          if(result.error != 201 && statusCode != 302 && statusCode != 200) {
            console.log("ERROR: Problem in CRM Post")
            console.log(result)
            return false;
          }else {
            console.log(result);
            var leadId = result['identity-profiles'].vid;
            User.findOne({email: email}, function(err, user2) {
              if (err) {
                return false;
              } else {
                if (user2) {
                  if (user2.email == email) {
                    var obj = {};
                    obj.ContactId = leadId;
                    user2.update(obj, function(err2, resul2) {
                      if (err2) {
                        console.log("ERROR: Adding CRM ID for "+email);
                        return false;
                      }
                      return true;
                    })
                  } else {
                    return false;
                  }
                }
              }
            });
            return true;
          }
        });
      }
    } else {
      console.log("ERROR: User " + user.email + " not in Database");
    }
  });

}

At this point I’m at a loss - both JSON objects run through JSONLint fine, and the only error I manage to get our of this is the 400 Bad Request, which makes me think that it’s a malformed postBody, but my logs show it being identically formed.

EDIT: Here is the output of my logging for a working call (API Key obscured for obvious reasons):

Sending accounts@gamebench.net to CRM:
{ host: 'api.hubapi.com',
  port: 443,
  path: '/contacts/v1/contact/?hapikey=xxxxx-xxxx-xxxxx-xxxxxxxxx',
  method: 'POST',
  headers: 
   { 'Content-Type': 'application/json',
     'Content-length': 447,
     Accept: 'application/json' } }

Request Path:
api.hubapi.com:443/contacts/v1/contact/?hapikey=xxxxx-xxxx-xxxx-xxxxxxx-xxxx

postBody Object:
{"properties":[{"property":"email","value":"testingapis@hubspot.com"},{"property":"firstname","value":"Adrian"},{"property":"lastname","value":"Mott"},{"property":"website","value":"http://hubspot.com"},{"property":"company","value":"HubSpot"},{"property":"phone","value":"555-122-2323"},{"property":"address","value":"25 First Street"},{"property":"city","value":"Cambridge"},{"property":"state","value":"MA"},{"property":"zip","value":"02139"}]}

And the same for the one that isn’t working:

Sending accounts@gamebench.net to CRM:
{ host: 'api.hubapi.com',
  port: 443,
  path: '/contacts/v1/contact/?hapikey=xxxx-xxxxx-xxxx-xxxxxx-xxxxxxxx',
  method: 'POST',
  headers: 
   { 'Content-Type': 'application/json',
     'Content-length': 434,
     Accept: 'application/json' } }

Request Path:
api.hubapi.com:443/contacts/v1/contact/?hapikey=xxxxxx-xxxx-xxxxx-xxxxxxxxx

postBody Object:
{"properties":[{"property":"email","value":"accounts@gamebench.net"},{"property":"firstname","value":"Testy"},{"property":"lastname","value":"McTester"},{"property":"gb_first_login","value":1486944000000},{"property":"gb_last_login","value":1487030400000},{"property":"gb_login_counter","value":2},{"property":"verification_email_counter","value":1},{"property":"gb_referral","value":"direct"},{"property":"ipaddress","value":"::1"}]}`

Any help with this would be appreciated,
J

0 Upvotes
7 Replies 7
gamebenchjake
Participant

Create Contact: 400 Bad Request

@pmanca Removing the IP address field was what fixed the issue for me a couple days ago. I don’t have the log file handy anymore, but the only change was me removing the:

{"property":"ipaddress","value":"::1"} 

Object from within the “properties” array.

0 Upvotes
3PETE
HubSpot Employee
HubSpot Employee

Create Contact: 400 Bad Request

@gamebenchjake I am thinking for the contact object the ip address is a read only property and can’t be updated. Unlike the forms API which allows you to do so.

0 Upvotes
gamebenchjake
Participant

Create Contact: 400 Bad Request

Looks like the issue was caused by my trying to use the “ipaddress” field - I created a custom one and it worked perfectly. Strange that nothing in the docs mentions this issue.

0 Upvotes
3PETE
HubSpot Employee
HubSpot Employee

Create Contact: 400 Bad Request

@gamebenchjake Can you expand upon the error you were getting out of curiosity? That isn’t a required field (ip address).

0 Upvotes
gamebenchjake
Participant

Create Contact: 400 Bad Request

Hi @pmanca ,

The error status code was a 400, and the message was “Bad Request”, a pretty generic error which is what made debugging it a bit of a pain (ended up just starting with First name, Last name, and Email and adding in the fields until it broke.)

I also tried with a couple random IPs just in case there was any validation parsing to no avail.

0 Upvotes
3PETE
HubSpot Employee
HubSpot Employee

Create Contact: 400 Bad Request

@gamebenchjake Can you share the 400 response? It should have a copy of the call that you made and how it was formatted.

0 Upvotes
3PETE
HubSpot Employee
HubSpot Employee

Create Contact: 400 Bad Request

@gamebenchjake Have you tried it with out the ip address?

0 Upvotes