APIs & Integrations

GaryElliott
メンバー

Getting a “400 Bad Request” when trying to create a deal

解決

Hi,

I'm creating deals in my Ruby on Rails application and everything's been working fine for months. But two days ago (22nd April 2019), I started getting a 400 Bad Request response despite making no changes to my application.

 

Has anything changed with the API?

 

My code looks like this:

 

endpoint = "https://api.hubapi.com/deals/v1/deal?hapikey=#{ENV["hubspot_api_key"]}"

payload = {
associations: {
associatedVids: [vid]
},
properties: [
{
value: "#{enquiry}",
name: "dealname"
},
{
value: "#{valid_internal_hubspot_reference}",
name: "dealstage"
},
{
value: "#{valid_internal_hubspot_reference}",
name: "pipeline"
}
]
}.to_json


uri = URI.parse(endpoint)
header = {'Content-Type': 'application/json'}
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
response = http.request(request)
return response

 

2件の承認済みベストアンサー
ivandhalluin
解決策
参加者

Getting a “400 Bad Request” when trying to create a deal

解決

 

Just switch from 

{'Content-Type': 'application/json'}

To 

{'Content-Type' => 'application/json'}

And it should be good 

 

元の投稿で解決策を見る

IsaacTakushi
解決策
HubSpot Employee
HubSpot Employee

Getting a “400 Bad Request” when trying to create a deal

解決

Hey, @GaryElliott and @ivandhalluin.

 

As Ivan found, HTTParty was improperly setting the Content-Type header with {'Content-Type': 'application/json'}.

 

From what I can tell, our team made an update to the Deals API on April 22, triggering 400 errors when Content-Type is invalid or omitted. Apologies for the confusion!

Isaac Takushi

Associate Certification Manager

元の投稿で解決策を見る

0 いいね!
10件の返信
ivandhalluin
参加者

Getting a “400 Bad Request” when trying to create a deal

解決

I have the exact same problem. The contact API is still working but the deal API stopped working 2 days ago and only answers 400 (with no details).

0 いいね!
IsaacTakushi
HubSpot Employee
HubSpot Employee

Getting a “400 Bad Request” when trying to create a deal

解決

Hi, @GaryElliott and @ivandhalluin.

 

I'm sorry to hear that you're encountering issues.

 

Can you confirm whether the same requests produce 400 errors in a REST client like Postman?

 

Additionally, could you each define the Hub IDs in which you are attempting to create deals and provide dummy payloads which are consistently throwing 400 errors in your Ruby applications? (If the results differ in a REST client, please let me know.)

Isaac Takushi

Associate Certification Manager
0 いいね!
ivandhalluin
参加者

Getting a “400 Bad Request” when trying to create a deal

解決

Ok, found the issue.
HTTParty was not sending the proper Content-Type : https://github.com/jnunemaker/httparty/issues/472
Switching to this syntax solved it for me:

headers: { 'Content-Type' => 'application/json' }

 
The thing is that it was working for the other endpoints (and this one, before the 22 of april) without sending the good Content-Type.

GaryElliott
メンバー

Getting a “400 Bad Request” when trying to create a deal

解決

I've been setting this header all along:

 

header = {'Content-Type': 'application/json'}

0 いいね!
ivandhalluin
解決策
参加者

Getting a “400 Bad Request” when trying to create a deal

解決

 

Just switch from 

{'Content-Type': 'application/json'}

To 

{'Content-Type' => 'application/json'}

And it should be good 

 

GaryElliott
メンバー

Getting a “400 Bad Request” when trying to create a deal

解決

Seemed to do the trick. So bizarre this would matter all of a sudden.

IsaacTakushi
HubSpot Employee
HubSpot Employee

Getting a “400 Bad Request” when trying to create a deal

解決

Nice find, @ivandhalluin!

 

That is strange. I'll ask the team if they have insight into why the behavior might have changed...

Isaac Takushi

Associate Certification Manager
0 いいね!
IsaacTakushi
解決策
HubSpot Employee
HubSpot Employee

Getting a “400 Bad Request” when trying to create a deal

解決

Hey, @GaryElliott and @ivandhalluin.

 

As Ivan found, HTTParty was improperly setting the Content-Type header with {'Content-Type': 'application/json'}.

 

From what I can tell, our team made an update to the Deals API on April 22, triggering 400 errors when Content-Type is invalid or omitted. Apologies for the confusion!

Isaac Takushi

Associate Certification Manager
0 いいね!
GaryElliott
メンバー

Getting a “400 Bad Request” when trying to create a deal

解決

Hi @IsaacTakushi ,

 

I don't get any errors when submitted via Postman.

 

Hub ID = 3818569

 

Payload:

 

{
"associations": {
"associatedVids": [
19328551
]
},
"properties": [
{
"value": "Test - Test",
"name": "dealname"
},
{
"value": "706ab8a3-219f-4557-a010-de1a0214d96b",
"name": "dealstage"
},
{
"value": "206d1d85-19d6-4c1b-877f-8342bef3e0b7",
"name": "pipeline"
}
]
}

 

Sending this payload in Postman doesn't return any errors.

 

 

0 いいね!
ivandhalluin
参加者

Getting a “400 Bad Request” when trying to create a deal

解決

It's the same thing for me, failing in Rails (using HTTParty). But working in postman with this body on 5651313 :

{"associations":{"associatedCompanyIds":[],"associatedVids":["2091301"]},"properties":[{"name":"pipeline","value":"default"},{"name":"dealstage","value":"appointmentscheduled"}]}

But the same methods (via Rails) are working for contacts (POST "/contacts/v1/contact").

0 いいね!