Forms API 400 bad request with C# HttpRequest

Hi there,

I’m new to using HubSpot, and am having trouble getting a successful POST to the API server.

I have setup an HttpRequest which sends some JSON data to the Forms API based on the example information in the API docs.

My request URL: https://api.hsforms.com/submissions/v3/integration/secure/submit/\/<formid>?hapikey=

The JSON string I am sending is formatted like so:

“{\“submittedAt\”:\“637583940787178861\”,\“fields\”:{\“home_club\”:\“test\”,\“firstname\”:\“test\”,\“lastname\”:\“qweqw\”,\“date_of_birth_datepicker___YYYY\”:\“1985\”,\“date_of_birth_datepicker___MM\”:\“01\”,\“date_of_birth_datepicker___DD\”:\“02\”,\“membership_number\”:\“12345\”,\“mobilephone\”:\“0123456789\”,\“email\”:\“test@test.com\”,\“cancellation_date__YYYY\”:\“2021\”,\“cancellation_date__MM\”:\“08\”,\“cancellation_date__DD\”:\“09\”,\“reason_for_cancellation\”:\“Medical\”,\“cancellation_terms___conditions\”:\“on\”}}”

I copied the field names from an embedded form that matches the formId, but for other reasons, I need to post this data using the API and not using the HubSpot embedded form.

Below is my C# reference code:

var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.ContentType = “application/json”;
httpWebRequest.Method = “POST”;

var fields = new Dictionary<string, string>();
fields.Add(…);

string json = JsonConvert.SerializeObject(new
{
submittedAt = new DateHelper().GetCurrentNZTime().Ticks.ToString(),
fields = fields
}) ;

using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
streamWriter.Write(json);
}

var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}

The response is coming back as 400 - Bad Request. HubSpot support has asked me to look here for help.

Thank you.

Hey @DMcLellan

That timestamp for submittedAt looks suspicious

Should be in milliseconds and can only be used to backdate a submission.

I would consider removing it from you body and see if that works. If it does and you need it, you will want to make sure that it is in milliseconds

Hi @dennisedson and thanks for the reply.

I have also been working with support on this and they too identified that the milliseconds portion of my data was incorrect.

I have modified my json string and now have the correct formatting. Below is the new formatting which is still giving me a 400 - Bad Request.

This is the string after JSON.stringify:

“{\“submittedAt\”:\“1622859625723\”,\“fields\”:[{\“name\”:\“home\”,\“value\”:\“test\”},{\“name\”:\“firstname\”,\“value\”:\“test\”},{\“name\”:\“lastname\”,\“value\”:\“qweqw\”},{\“name\”:\“date_of_birth_datepicker_\”,\“value\”:\“473490000000\”},{\“name\”:\“membership_number\”,\“value\”:\“12345\”},{\“name\”:\“mobilephone\”,\“value\”:\“0123456789\”},{\“name\”:\“email\”,\“value\”:\“test@test.com\”},{\“name\”:\“cancellation_date\”,\“value\”:\“1628481600000\”},{\“name\”:\“cancellation_terms___conditions\”,\“value\”:\“true\”}]}”

Any more thoughts on this?

Thank you very much.

@DMcLellan

Was that date in the past when you submitted it?

Can you try backdating atlease 24hrs as well as attempting to submit without that date altogether?

Hi again @dennisedson and thanks for the reply.

As it turned out through digging through the API docs. When submitting a post to the Forms API, the email field must validate to a real email address. I was testing submissions using test@test.com and originally example@example.com since this is what HubSpot has listed in the docs as example JSON data. It’s a bit contradictory, but that turned out to be the issue.

I appreciate your help, though.

This post can be archived and considered resolved.