Created
May 29, 2019 14:25
-
-
Save lizardking8610/f366c981c6f64d83199cc9ef2a1c73d6 to your computer and use it in GitHub Desktop.
Gravity Forms API POST example
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
| <script src="https://code.jquery.com/jquery-1.10.2.js"></script> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/hmac-sha1.js"></script> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/components/enc-base64-min.js"></script> | |
| <script type="text/javascript"> | |
| function CalculateSig(stringToSign, privateKey){ | |
| //calculate the signature needed for authentication | |
| var hash = CryptoJS.HmacSHA1(stringToSign, privateKey); | |
| var base64 = hash.toString(CryptoJS.enc.Base64); | |
| return encodeURIComponent(base64); | |
| } | |
| //set variables | |
| var d = new Date; | |
| var expiration = 3600; // 1 hour, | |
| var unixtime = parseInt(d.getTime() / 1000); | |
| var future_unixtime = unixtime + expiration; | |
| var publicKey = "{key here}"; | |
| var privateKey = "{key here}"; | |
| var method = "POST"; | |
| var route = "forms/{form id here}/submissions"; | |
| stringToSign = publicKey + ":" + method + ":" + route + ":" + future_unixtime; | |
| sig = CalculateSig(stringToSign, privateKey); | |
| var url = 'http://domain.com/gravityformsapi/' + route + '?api_key=' + publicKey + '&signature=' + sig + '&expires=' + future_unixtime; | |
| // input ID's on form (within GF editor) | |
| var values = {input_values : { | |
| input_1 : 'John', | |
| input_2 : 'Smith', | |
| input_5 : '[email protected]', | |
| input_4 : 'Message goes here...' | |
| } | |
| } | |
| //json encode array | |
| values_json = JSON.stringify(values); | |
| $.post(url, values_json, function(data){ | |
| console.log(data.response); | |
| }); | |
| </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment