Created
August 16, 2018 21:33
-
-
Save NickDeckerDevs/ad3eef97b1f15e54df9f4d0d760de216 to your computer and use it in GitHub Desktop.
node js hubspot form submission using http request
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // We need this to build our post string | |
| const querystring = require('querystring'); | |
| const http = require('http'); | |
| const https = require('https'); | |
| function getFormData(data) { | |
| var hsFormData = JSON.stringify({ | |
| "fields": [{ | |
| "name": "email", | |
| "value": data.email | |
| }, | |
| { | |
| "name": "twitterhandle", | |
| "value": data.twitter || " " | |
| }, | |
| { | |
| "name": "country", | |
| "value": data.country | |
| }, | |
| { | |
| "name": "i_agree_to_receive_periodic_emails", | |
| "value": data.optin || "false" | |
| }, | |
| { | |
| "name": "something_else", | |
| "value": data.book || "false" | |
| }, | |
| { | |
| "name": "soemthign_or_another", | |
| "value": data.something || "false" | |
| }, | |
| { | |
| "name": "subscribe_to_the_blog_by_email_checkbox", | |
| "value": "false" | |
| } | |
| ], | |
| "context": { | |
| "pageUri": "www.square2marketing.com/automation-supreme-machine", | |
| "pageName": "Penguin Awesome Node.js App" | |
| }, | |
| "skipValidation": "true" | |
| }); | |
| return hsFormData; | |
| } | |
| function sendToHubspot(formData, done) { | |
| // Build the post string from an object | |
| let hubId = 12312; | |
| let formId = 'asdfasdf-sdfasdf-sadf-asfd-asf'; | |
| let sendData = getFormData(formData); | |
| var post_options = { | |
| host: 'api.hsforms.com', | |
| port: '80', | |
| path: '/submissions/v3/integration/submit/' + hubId + '/' + formId, | |
| method: 'POST', | |
| headers: { | |
| 'Content-Type': 'application/json' | |
| } | |
| }; | |
| var req = http.request(post_options, function(res) { | |
| var responseString = ''; | |
| res.on('data', function(data) { | |
| responseString += data; | |
| }); | |
| res.on('end', function() { | |
| console.log(responseString); | |
| // checks for data in the return value | |
| if(responseString.indexOf('Nicholas') > -1) { | |
| return done(); | |
| } | |
| }); | |
| }); | |
| req.write(sendData); | |
| req.end(); | |
| } | |
| module.exports.sendToHubspot = sendToHubspot; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment