APIs & Integrations

trose0494
Member

Trouble Sending form data to a serverless function

Hello

 

What I'm trying to do is submit an HTML form and have my serverless function handle the data sent over then return the result of the function to the page 

 

I referenced this from the serverless function documentation: 


Using serverless functions with the form element
When submitting serverless functions use javascript to handle the form submission, and use the "contentType" : "application/json" header in your request. Do not use the <form> elements action attribute.


and did set up my jquery to look like this: 

 

$.post('https://6326255.hs-sites.com/_hcms/api/update-acc-order', data)
	.then(function (response) {
		console.log(response) })
	.fail(function(xhr, textStatus, error) {
		console.log(xhr.statusText);
		console.log(textStatus);
		console.log(error.responseJSON) });

The response I get back is: Request Forbidden after the page reloads

 
I'm wondering am I setting something up incorrectly?

 

Also, how does the serverless function handle the data it' been sent is there a default package that can handle the request, or will I need to find one?

 

Thanks for any and all the help!!! 

0 Upvotes
3 Replies 3
IsaacTakushi
HubSpot Employee
HubSpot Employee

Trouble Sending form data to a serverless function

Welcome, @trose0494.

 

Apologies for the delayed response.

 

I took a look at your serverless.json file and noticed that you changed the update-acc-order endpoint to acc-order shortly after posting this question. Are you still encountering 403 errors with the new endpoint URL?

 

If so, are you able to make successful one-off POST requests in a REST client like Postman? If not, do you have a sample JSON payload I can test with?

 

Per your second question, HubSpot serverless functions do not use a default package to handle incoming POST requests. It looks like you've already figured out a way to handle these in update-acc-order.js, though. Am I correct?

Isaac Takushi

Associate Certification Manager
0 Upvotes
trose0494
Member

Trouble Sending form data to a serverless function

Hi Isaac,

 

Yes I changed my endpoint I'm no longer getting the 403 error but I am getting a 400 Bad Request error now. 

 

I haven't tested with Postman but here is some JSON you can test with:  

0:{acessory: "71314"}

 

I did find a way to process the data in the serverless function. 

 

Thanks!,

0 Upvotes
IsaacTakushi
HubSpot Employee
HubSpot Employee

Trouble Sending form data to a serverless function

Hi, @trose0494.

 

Thanks for clarifying.

 

I changed the format of the body and spelling of "acessory" to:

{
    "accessory": "71314"
}

and made a POST request directly in Postman. It does return a 400 Bad Request response but the empty validation message ({}) suggests the error lies within the update-acc-order.js script itself.

Isaac Takushi

Associate Certification Manager
0 Upvotes