I'm not very literate in writing posts to web services, so apologies for the naivete, but am trying to figure out a way to create a contact in HubSpot using this module, based on a rules event. I've been able to run a successful GET operation using a simple API call as documented here https://developers.hubspot.com/docs/methods/get-account-details. In this rules action, everything is in the url, nothing in the headers or data fields. The response is returned successfully.

But anytime I try a POST, trying to use this method documented here https://developers.hubspot.com/docs/methods/contacts/create_contact, I get numerous Drupal errors (below). I am putting the post url in the url field (e.g. https://api.hubapi.com/contacts/v1/contact/?hapikey=[key]), leaving the header field blank, and trying to put something like this in the data field:

{
  "properties": [
    {
      "property": "email",
      "value": "email@gmail.com"
    },
    {
      "property": "firstname",
      "value": "Michelle"
    }
    ]
}

I've also tried something like this, but still get the same errors.

email=email@gmail.com&firstname=Michelle

Here are the errors that appear when trying to make this call:

Notice: Undefined offset: 1 in rules_http_client_request_url() (line 111 of /srv/bindings/d67615887a3549bd84f8672d8e65296d/code/sites/all/modules/rules_http_client/rules_http_client.rules.inc).
Notice: Undefined variable: process_data in rules_http_client_request_url() (line 115 of /srv/bindings/d67615887a3549bd84f8672d8e65296d/code/sites/all/modules/rules_http_client/rules_http_client.rules.inc).
Warning: strlen() expects parameter 1 to be string, array given in drupal_http_request() (line 930 of /srv/bindings/d67615887a3549bd84f8672d8e65296d/code/includes/common.inc).
Notice: Array to string conversion in drupal_http_request() (line 955 of /srv/bindings/d67615887a3549bd84f8672d8e65296d/code/includes/common.inc).
Notice: Undefined offset: 1 in rules_http_client_request_url() (line 97 of /srv/bindings/d67615887a3549bd84f8672d8e65296d/code/sites/all/modules/rules_http_client/rules_http_client.rules.inc).
Notice: Undefined offset: 1 in rules_http_client_request_url() (line 111 of /srv/bindings/d67615887a3549bd84f8672d8e65296d/code/sites/all/modules/rules_http_client/rules_http_client.rules.inc).
Notice: Undefined variable: process_data in rules_http_client_request_url() (line 115 of /srv/bindings/d67615887a3549bd84f8672d8e65296d/code/sites/all/modules/rules_http_client/rules_http_client.rules.inc).
Warning: strlen() expects parameter 1 to be string, array given in drupal_http_request() (line 930 of /srv/bindings/d67615887a3549bd84f8672d8e65296d/code/includes/common.inc).
Notice: Array to string conversion in drupal_http_request() (line 955 of /srv/bindings/d67615887a3549bd84f8672d8e65296d/code/includes/common.inc).

I confirmed I can make this call successfully to HubSpot using the Chrome extension Sense when it looks like this:

POST contacts/v1/contact/?hapikey=[key]
{
  "properties": [
    {
      "property": "email",
      "value": "email@gmail.com"
    },
    {
      "property": "firstname",
      "value": "Michelle"
    }
    ]
}

Can someone please point me in the right direction? What is the format I need to use in the data field to post the above?

Comments

michellezeedru created an issue.

jsibley’s picture

If someone could post an example of a properly formatted body, that would be great. I'm not sure if and where I'm supposed to use quotes.

Looking at this some more, I think my biggest question is how to handle an array, where in Postman, for example, I could have:

"params": {
"admin": "admin",
"password": "my_password" }

What would the proper syntax for this be in Data?

And, is there any easy way to see what the rule is actually sending?

Many thanks!

la_by’s picture

Hello, there was a similar problem, did you find a solution?

adigunsherif’s picture

If you don't mind enabling the PHP filter core module, you could simply use the Drupal drupal_http_request(). Just configure your rules as usual and choose the action EXECUTE PHP. then paste this in there

//define the content to send (converting from json to php

$thecontent = array(
  "properties" => array(
    array(
      "property" => "email",
      "value" => "email@gmail.com"
    ),
     array(
      "property" => "firstname",
      "value" => "Michelle"
    )
    )
);
$url = "contacts/v1/contact/?hapikey=[key]";

// use drupal drupal_http_request to send a post request 

$send_message = drupal_http_request($url, array(
	'headers' => array('Content-Type' => 'application/json'),
	'method' => 'POST',
	'data' => json_encode($thecontent)
));

that's done. If you have devel module installed, you can check the result if success or error by doing dpm($send_message); hope it helps

la_by’s picture

And how to send if there is a given json - but in the format post raw data?

{
    "aCode": "xxx",
    "importdata": {
        "id": "xxx",
        "user_data": {
            "name": "xxx",
        },
        "contact": {
            "phone": "xxx",
        }
    }
}
adigunsherif’s picture

Same way. Just convert { to array() and "key": " value" to "key" => " value", and you are good. Follow my earlier example.

Let me know if you need more help