0

I am trying to connect Hubspot Form with nodejs and is gives me an error. I have test in my local server as well as on remote heroku server. here is my code.

app.post('/contact', (req, res) => {
    //require Node modules
    var https = require('https')
    var querystring = require('querystring')

    // global variables
    const HUB_ID = "My_HUB_ID";
    const FORM_GUID = "MY_FORM_ID";

    var postData = querystring.stringify({
        'email': "test@gmail.com",
        'firstname': "Test User",
        'phone': "03001234567",
        'message': "Hello World!!!",
        'hs_context': JSON.stringify({
            "hutk": '',
            "ipAddress": req.headers['x-forwarded-for'] || req.connection.remoteAddress,
            "pageUrl": "/contact",
            "pageName": "Contact"
        })
    })

    var options = {
        hostname: 'forms.hubspot.com',
        path: '/uploads/form/v2/' + HUB_ID + '/' + FORM_GUID,
        method: 'POST',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'Content-Length': postData.length
        }
    }

    var request = https.request(options, function(response){
        console.log("Status: " + response.statusCode)
        console.log("Headers: " + JSON.stringify(response.headers))
        response.setEncoding('utf8')
        response.on('data', function(chunk){
            console.log('Body: ' + chunk)
        });
    });

    request.on('error', function(e){
        console.log("Problem with request " + e.message)
    });

    // post the data
    request.end();

    res.json({"message": "Welcome to Bazaristan E-Commerce Online web portal"});
});

The interesting part is tested this code in php and it work fine. I receive the following error:

Problem with request Error: read ECONNRESET

1 Answers1

0

Try using this library instead of using the raw API - it has always worked for us. If it still doesn't work, then try adding debugging as described in the answers in this SO question.

itoctopus
  • 4,133
  • 4
  • 32
  • 44