Submitting Form Data throught API



Im running into similar troubles when trying to submit form data through your API.
We’re trying to accumulate data from our backend that then sends information forward to the Hubspot Contact Form. The data arrives with a ‘204’ as expected and is not returning any error.

However, when you do look at the data, its not connecting it with the traffic the user/contact has generated.

This is what our simple example looks like:

public class HubspotTrackingService {

String name;
String lastname;
String phone;
String email;
String company;
//Hubspot Tracking Cookie identifier
String hutk;
String ip;

// log
private static final Logger log = Logger.getLogger(RegistrationApplicationService.class.getName());

public HubspotTrackingService(String name, String lastname, String phone, String email, String company, String hutk, String ip){ = name;
    this.lastname = lastname; = phone; = email; = company;
    this.hutk = hutk;
    this.ip = ip;

public void send() throws Exception {

    String url = PortalProperties.REGISTRATION_TRACKINGURL_HUBSPOT.getValue();
    try {

        String[] params = new String[]{
                "company", company,
                "firstname", name,
                "lastname", lastname,
                "phone", phone,
                "email", email,
                URLEncoder.encode("hs_context", "UTF-8").toString(),
                        new JSONObject().put("hutk", hutk)
                                        .put("pageUrl", "[REDACTED FOR PRIVACY]")
                                        .put("pageName", "[REDACTED FOR PRIVACY]")
                                        .put("ipAddress", ip).toString()
        String[] headers = new String[]{
                "Content-Type", "application/x-www-form-urlencoded",
        URLPost urlPost = new URLPost();
        URLPost.URLPostResult result = urlPost.postWithResult(url, false, headers, true, params);

        if (result.code == 204 && Comparison.isVoid( {
            // it worked.
        } else {
            throw new Exception("Tracking call returned with HTTP status " + result.code);

    } catch(Exception e){
        log.log(Level.SEVERE, "Failed to call registration tracking endpoint " + url, e);

public static void main(String[] args){
    HubspotTrackingService trackingService = new HubspotTrackingService("testing", "internal", "123456789",
                            "", "company gmbh", "d27029cbe32192da89b7a4539fc0ebda", "[REDACTED FOR PRIVACY]");

    try {
    } catch(Exception e){
        log.log(Level.SEVERE, e.getMessage());


The post function:

public URLPostResult postWithResult(String url, boolean noSevereOnErrorCode, String[] headers, boolean contentLengthHeader, String... kvpairs) throws Exception {
    // Construct data
    StringBuilder input = new StringBuilder();
    for (int i = 0; i < kvpairs.length; i += 2) {
        String key = kvpairs[i];
        String value = kvpairs[i + 1];
        if (i != 0) {
        input.append(URLEncoder.encode(key, "UTF-8")).append("=").append(URLEncoder.encode(value, "UTF-8"));

    // Send data
    URLConnection conn = openConnection(url);

    if (headers != null) {
        for (int i = 0; i < headers.length; i += 2) {
            conn.addRequestProperty(headers[i], headers[i + 1]);
            conn.addRequestProperty("Content-Length", String.valueOf(input.length()));

    if (input.length() > 0) {
        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
        try {
        } finally {

    int code = HttpURLConnection.HTTP_OK;
    if (conn instanceof HttpURLConnection) {
        try {
            code = ((HttpURLConnection) conn).getResponseCode();
        } catch (IOException ex) {
            // If the return code is 503 or something, we don't get this, but an IOException instead
            code = HttpURLConnection.HTTP_INTERNAL_ERROR;
        if (code != HttpURLConnection.HTTP_OK) {
            Logger.getLogger(URLPost.class.getName()).log(noSevereOnErrorCode ? Level.WARNING : Level.SEVERE, "failed to access url \"" + url + "\" code=" + code);

    // Get the response
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    try {
        if ((conn instanceof HttpURLConnection) && (code == HttpURLConnection.HTTP_NOT_FOUND)) {
            HttpURLConnection httpURLConnection = (HttpURLConnection) conn;
            FileUtil.copy(httpURLConnection.getErrorStream(), output);
        } else {
            try {
                FileUtil.copy(conn.getInputStream(), output);
            } catch (Exception ex) {
                HttpURLConnection httpURLConnection = (HttpURLConnection) conn;
                FileUtil.copy(httpURLConnection.getErrorStream(), output);

    } catch (Exception ex) {
        Logger.getLogger(URLPost.class.getName()).log(Level.SEVERE, "failed to read result stream", ex);

    URLPostResult res = new URLPostResult(url, code, output.toString("UTF-8"));

    return res;

The request looks like this (encoded):

Any help would be greatly appreciated.



Hi @paul09,

That encoded request appears to be formatted correctly. Can you send me a link to an example contact in your portal so that I can look at the raw form submission data as it appears in HubSpot?


Hi Derek,

The contact in question can be found here:


Hi @paul09,

It looks like the usertoken was successfully included in the form submission, but I’m still not seeing any page views. This often means the hubspotutk cookie was cleared from the visitor’s browser; can you double check that the token you’ve submitted with the form submission is the same one that the contact has in their browser?


Hi @Derek_Gervais,

That particular contact has been a user contact I have used for debugging purposes only. That hubspotutk is the one I get set as soon as I enter our domain, it gets updated by each page view (to the same value). I can guarantee that it is not being cleared/purged/removed.

Here’s proof that it is being set to the exact same value by each page view(wait for it, its a gif):

To be noted is that this is happening to all of our contacts that we submit using the API call. We have a form submitted here:

Appreciate any help I can get here.


Hi @paul09,

Thank you for the example, that looks as I would expect it to. Is this a single page app? Is the HubSpot tracking code is present and firing on these pages? Everything looks correct internally (other than the lack of page view data)


Hey @Derek_Gervais,

The webpage is not a single page app. The tracking cookie is there and firing/being updated for each website, im sort of out of ideas what else I can do.


Hi @paul09,

When you say tracking cookie, are you referring to the HubSpot tracking code, or the hubspotutk cookie? Both need to be present for a page view to be tracked. If a visitor views a page that doesn’t have the HubSpot tracking code installed, the views will not be tracked. If you send me a link to a page that you’re visiting I can take a look to see if the tracking code is installed.

Side note: after examining some recent submissions to that form, I noticed that the contacts do not have a usertoken associated with them. Unlike the first test contact you sent, the raw form submission data from the other submissions doesn’t appear to include a hutk value (or any metadata, for that matter). I’m not entirely sure why only that one contact included the relevant metadata, but for those contacts the issue appears to be that the hs_context parameter was missing/malformed.


Hi @Derek_Gervais,

Thanks for your reply. Both the __hstc and the hubspotutk are present on the webpage.
Ill PM you the link so you can have a look. That a lot of the contacts are malformed is something we are aware of, that comes down to the hs_context being malformed.