Getting 400 Bad Request when trying to add a Blog Post

Highlighted
Occasional Contributor

Hi guys,

 

i have some trouble trying to create Blog Posts, i would like to receive a JSON response, but i have a 400 Bad Request error.

I've done the same code with Contact model, using the v1 of HubSpot API and it is working well. The same code for Blog Posts doesn't go through and i don't understand why

i am using Java 8 and RestTemplate.

 

So, with Postman straight to the HubSpotAPI and the following datas it works:
URL: https://api.hubapi.com/content/api/v2/blog-posts?hapikey=*****************
Header: Content-Type: application/json
Body

 

{
"name": "My first API blog post!",
"content_group_id": 351076997
}

 

==> Here is the Response, nice:

{
    "correlationId": "************************",
    "errorType": "PARENT_BLOG_DOES_NOT_EXIST",
    "message": "Selected parent blog doesn't exist",
    "requestId": "************************",
    "status": "error"
}

Now, when i am using Java to create the request

Through my service, it doesn't works:
URL: http://localhost:8888/hubspot/blogposts?hapikey=************************
Headers & Request:
// Header and content declarations
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<BlogpostSendPost> request = new HttpEntity<>(blogpostSendPost, headers);
// Tried also
// HttpEntity<String> request = new HttpEntity<>(blogpostSendPost.toJson(), headers);
In my Java code, i am calling the API in this way (Object type, is for dev needs)
Object answer = restTemplate
.postForObject("https://api.hubapi.com/content/api/v2/blog-posts?hapikey=****************", request,
Object.class);
==> Here is the Reponse, why ?:
{
    "timestamp": "2019-12-09T19:10:01.981+0000",
    "status": 500,
    "error": "Internal Server Error",
    "message": "400 Bad Request",
    "trace": "org.springframework.web.client.HttpClientErrorException$BadRequest: 400 Bad Request\r\n\tat 
.........
}


I have read the other posts about the header like this one:
https://community.hubspot.com/t5/APIs-Integrations/Getting-a-400-Bad-Request-when-trying-to-create-a...
Is there anything in the API v2 changing the way to receive the JSON body, i should add to my code to make it works ?

Thank you for your help Smiley Very Happy

Romain

Reply
0 Upvotes
6 Replies 6
Highlighted
Community Manager

Hey @romaindalichamp,

 

Based on my research, it appears that this error is because of wrong headers or wrong request body. Looking at the header it looks fine. One thing that I noticed though is that the code didn't include the HTTP method POST. Could we try that out and see if it works?

 

Reference this discussion here: https://www.concretepage.com/questions/517.

Highlighted
Occasional Contributor

Hey WendyGoh,

 

Thank you for your anwser.

I am already using "PostForObject" from the class RestTemplate for POST requests, but just to be sure, i changed my code with the following lines, but i still have the same response 400:

Object answer = restTemplate.exchange(
"https://api.hubapi.com/content/api/v2/blog-posts?hapikey=**********",
HttpMethod.POST,
request,
Object.class);

The body i am using is the example from here: https://developers.hubspot.com/docs/methods/blogv2/post_blog_posts

 

So i guess, my problem should com from the header ...

 

Here is exactly what is send when i log the request, a step before the action:

 <{"name":"My first API blog post!","content_group_id":"351076997"},[Content-Type:"application/json"]>

 

 

i am trying to check if the solution of ivandhalluin can work in my case, but i don't see how to do that ...

Reply
0 Upvotes
Highlighted
Occasional Contributor

Ok, i found out a way to make it work but it seems something is going wrong, i have the same parameters & values for this cases:

- Contacts + HubSpot API v1 + RestTemplate ==> Post Request = OK

- Blog Post + HubSpot API v2 + RestTemplate ==> Post Request = NOK

- Blog Post + HubSpot API v2 + UniRest ==> Post Request = OK

 

What do you think ? is the header formatted differently in an incompatible way with RestTemplate and your API v2 ?


Check theses samples: 

log.info("=====RestTemplate===================================================");
// Header and content declarations
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);

// TRY 1: CONTACTS - RestTemplate - OK - contact is created (API V1)
HttpEntity<ContactSendList> request1 = new HttpEntity<>(contactSendList, headers);
ContactResponseInformations answer1 = restTemplate
.postForObject(
HubSpotConfiguration.URL_CREATE_CONTACT, // https://api.hubapi.com/content/api/v2/blog-posts?hapikey=*************
request1,
ContactResponseInformations.class);
log.info(answer1.toString()); // OK

// TRY 2: BLOG POSTS - RestTemplate - 400 Bad Request
// "Object", is just for sample
HttpEntity<String> request2 = new HttpEntity<>(blogpostSendPost.toJson(),headers);
Object answer2 = restTemplate
.postForObject(
HubSpotConfiguration.URL_CREATE_BLOGPOST, // https://api.hubapi.com/content/api/v2/blog-posts?hapikey=*************
request2,
Object.class);
log.info(answer2.toString()); // 400 Bad Request

// TRY 3: BLOG POSTS - RestTemplate - 400 Bad Request
// "Object", is just for sample
HttpEntity<String> request3 = new HttpEntity<>(blogpostSendPost.toJson(),headers);
Object answer3 = restTemplate.exchange(
HubSpotConfiguration.URL_CREATE_BLOGPOST, // https://api.hubapi.com/content/api/v2/blog-posts?hapikey=*************
HttpMethod.POST,
request3,
Object.class);
log.info(answer3.toString()); // 400 Bad Request

log.info("=====UNIREST===================================================");
// TRY 4: BLOG POSTS - UNIREST - OK
HttpResponse<JsonNode> resp = Unirest
.post(HubSpotConfiguration.URL_CREATE_BLOGPOST) // https://api.hubapi.com/content/api/v2/blog-posts?hapikey=*************
.header("Content-Type", "application/json")
.body(blogpostSendPost)
.asJson();
log.info(resp.getBody().getObject().toString()); // OK

 Thank you for your help !

Reply
0 Upvotes
Highlighted
Community Manager

Hey @romaindalichamp,

 

When looking at TRY 1 and TRY 2, I noticed something different in the way you set the POST body to JSON. For TRY 1, you aint' using the toJson() method whereas for TRY 2 you're using the to.Json() method. 

 

Curious to know if there are any differences between the way you convert contactSendList and blogpostSendPost to JSON?

Reply
0 Upvotes
Highlighted
Occasional Contributor

Hi WendiGoh,

 

Thank your for your anwser.

Theses toJson(). are totally the same code and optionals. 

 

This line can be replace:

HttpEntity<String> request2 = new HttpEntity<>(blogpostSendPost.toJson(), headers);
log(request2.toString()); //<{"name":"My first API blog post!","content_group_id":"351076997"},[Content-Type:"application/json"]>

with

 

 

HttpEntity<BlogpostSendPost> request2 = new HttpEntity<>(blogpostSendPost, headers);
log(request2.toString()); //<BlogpostSendPost(name=My first API blog post!, content_group_id=351076997),[Content-Type:"application/json"]>

and it change nothing to the response i get from the API (OK ou 400 depending on the 4 Try)

 

 

 

Reply
0 Upvotes
Occasional Contributor

Hi Wendy,

 

i still have the problem, did you find any informations about that topic ?

 

Thank you for your help.

 

Romain

Reply
0 Upvotes