Submit Form API with Ruby on Rails - Response 204 but no content

I’m having an issue with the submit form API (https://developers.hubspot.com/docs/methods/forms/submit_form).

I’m getting a 204 response. But there is a message with ‘No content’. The form is getting submissions but they are all ‘No Contact Record’.

Here is the code from my controller:

payload = { email: params[:email],
 	firstname: params[:first_name],
 	lastname: params[:last_name],
 	venue: params[:venue],
 	product_type: params[:product_type],
 	hs_context: { hutk: cookies[:hubspotutk],
 	 ipAddress: request.remote_ip,
 	 pageUrl: request.url,
 	 pageName: "EnquiryForm" } 
 }

	endpoint = "https://forms.hubspot.com/uploads/form/v2/#{ENV['hubspot_portal_id']}/#{ENV['hubspot_enquiry_form_GUID']}"

	begin
 uri = URI.parse(@endpoint)
 header = header = {'Content-Type': 'application/x-www-form-urlencoded'}
 http = Net::HTTP.new(uri.host, uri.port)
 http.use_ssl = true
	 http.verify_mode = OpenSSL::SSL::VERIFY_NONE
 request = Net::HTTP::Post.new(uri.request_uri, header)
 request.body = payload.to_json
 response = http.request(request)
 response_output = "response #{response.body}"
 rescue => e
 response_output = "failed: #{e}"
 end

Here is the header and body of my request:

header:
 :content-type:
 - application/x-www-form-urlencoded
 accept-encoding:
 - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
 accept:
 - "*/*"
 user-agent:
 - Ruby
 connection:
 - close
 host:
 - forms.hubspot.com
 content-length:
 - '276'
 content-type:
 - application/x-www-form-urlencoded
body: '{"email":"test@test.com","firstname":"Test","lastname":"Test","venue":"testing","product_type":"test","hs_context":{"hutk":"d68c5983a0f224a8c87f113856466184","ipAddress":"127.0.0.1","pageUrl":"http://localhost:3000/enquiry","pageName":"EnquiryForm"}}'
body_stream: 
body_data:

I’m at a bit of a loss on what to change. Any suggestions?

Hi @GaryElliott,

Can you send me a link to the form you’re referring to in HubSpot so that I can take a closer look? Also, I’m not particularly familiar with Ruby; are the form fields (email, firstname, etc.) being put into a JSON object? They should be URI encoded fields, since the endpoint doesn’t accept JSON.

developers.hubspot.com

Submit data to a form | HubSpot Forms API

POST https://forms.hubspot.com/uploads/form/v2/:portal\_id/:form\_guid - Send form submission data to HubSpot. Form submissions from external sources can be made to any registered HubSpot form. You can see a list of forms on your portal by going to…

Hi Derek,

I’m currently using a sandbox to test.

Here’s the form.

And yes, all values in the payload are URI encoded. This includes form values as well as those set in hs_context

Solved it.

Turns out, the whole payload doesn’t need to be JSON.

This is what worked:

hs_context = { hutk: cookies[:hubspotutk],
 ipAddress: request.remote_ip,
 pageUrl: request.url,
 pageName: "EnquiryForm" 
 }.to_json

 payload = URI.escape("email=#{params[:email]}&firstname=#{params[:first_name]}&lastname=#{params[:last_name]}&venue=#{params[:venue]}&product_type=#{params[:product_type]}&hs_context=#{hs_context}")