2

What is the difference with UniRest and Spring RestTemplate which is giving back a 400 Bad Request with apparently the same header and body sent ?

I try to reach the HubSpot API to create a BlogPost, but using RestTemplate I have a 400 Bad Request error, and using UniRest works alright (returns an OK response). However, I do not want to include a library just to make one REST call: I'd rather stick to RestTemplate.


The request data I need to send

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

Using RestTemplate

Setting up the request:

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

OR in JSON

The .json() method converts my object in Json like you can see in the logs

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

With .postForObject(): 400 Bad Request

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

With .exchange(): 400 Bad Request

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

Using UniRest: OK

HttpResponse<JsonNode> resp = Unirest
        .post("https://api.hubapi.com/content/api/v2/blog-posts?hapikey=**********")
        .header("Content-Type", "application/json")
        .body(blogpostSendPost)
        .asJson();

I am using PostMan to call my REST SpringBoot Application which is using theses Services : when I am calling the HubSpot API directly from PostMan it works fine, just like with UniRest lib.

Thanks for your help guys !!

  • 1
    If you surround the `RestTemplate` calls with a `try/catch` that checks `RestClientResponseException`, does it enter that `catch`? – payne Dec 14 '19 at 17:02
  • 1
    Another thing you could try would be to replace `BlogpostResponsePost answer = restTemplate.exchange(...)` with `ResponseEntity answer = restTemplate.exchange(...)` and then extract the information from `answer` appropriately. – payne Dec 14 '19 at 17:09
  • Another possibility: if the `POST` is actually trying to create a resource, then maybe you tried with `Unirest` in the first place, which worked fine, and *then* tried with `RestTemplate`, which returned an error because the ID was already taken? – payne Dec 17 '19 at 04:06
  • Are you doing anything with the `RestTemplate` instance before calling the `exchange` method ? Also, you can try adding some more logging to see what is going on : https://stackoverflow.com/questions/7952154/spring-resttemplate-how-to-enable-full-debugging-logging-of-requests-responses – Olivier L. Applin Dec 17 '19 at 04:40
  • Oh and doesn't the call to `exchange` should return a `ResponseEntity` ? – Olivier L. Applin Dec 17 '19 at 04:43
  • Could you provide exactly the error message/exception that you encounter ? – Olivier L. Applin Dec 17 '19 at 05:08

1 Answers1

0

Please refer https://community.hubspot.com/t5/APIs-Integrations/Getting-400-Bad-Request-when-trying-to-add-a-Blog-Post/td-p/306532

Instead of converting request object to json, pass request object directly. It worked for me.

// TRY 1: CONTACTS - RestTemplate - OK - contact is created (API V1)

HttpEntity request1 = new HttpEntity<>(contactSendList, headers); ContactResponseInformations answer1 = restTemplate .postForObject( HubSpotConfiguration.URL_CREATE_CONTACT, request1, ContactResponseInformations.class); log.info(answer1.toString()); // OK

  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/30137438) –  Oct 21 '21 at 10:04