简体   繁体   中英

What is the difference between UniRest and Spring RestTemplate giving an http 400 Bad Request?

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 !!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM