title: What is the difference between UniRest and Spring RestTemplate giving an http 400 Bad Request?
body: <p>What is the difference with <strong>UniRest</strong> and Spring <strong>RestTemplate</strong> which is giving back a 400 Bad Request with apparently <strong>the same header and body sent ?</strong></p>
  
  <p>I try to reach the HubSpot API to create a BlogPost, but using <code>RestTemplate</code> I have a 400 Bad Request error, and using <code>UniRest</code> 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 <code>RestTemplate</code>.</p>
  
  <hr>
  
  <h1>The request data I need to send</h1>
  
  <ul>
  <li><strong>HttpMethod:</strong> <code>POST</code></li>
  <li><strong>URL:</strong> <a href="https://api.hubapi.com/content/api/v2/blog-posts?hapikey=" rel="nofollow noreferrer">https://api.hubapi.com/content/api/v2/blog-posts?hapikey=</a>*****************</li>
  <li><strong>Header:</strong> <code>Content-Type: application/json</code></li>
  <li><strong>Body:</strong> (represented by a class instance as <code>blogpostSendPost</code> further down)</li>
  </ul>
  
  <pre><code>{
    "name": "My first API blog post!",
    "content_group_id": 351076997
  }
  </code></pre>
  
  <hr>
  
  <h1>Using <code>RestTemplate</code></h1>
  
  <p>Setting up the request:</p>
  
  <pre class="lang-java prettyprint-override"><code>HttpHeaders headers = new HttpHeaders();
  headers.setContentType(MediaType.APPLICATION_JSON);
  HttpEntity&lt;BlogpostSendPost&gt; request = new HttpEntity&lt;&gt;(blogpostSendPost, headers);
  log(request.toString()); 
  //LOG PRINT: &lt;BlogpostSendPost(name=My first API blog post!, content_group_id=351076997),[Content-Type:"application/json"]&gt;
  </code></pre>
  
  <p>OR in JSON</p>
  
  <p><em>The .json() method converts my object in Json like you can see in the logs</em></p>
  
  <pre class="lang-java prettyprint-override"><code>HttpEntity&lt;String&gt; request = new HttpEntity&lt;&gt;(blogpostSendPost.toJson(), headers);
  log(request.toString()); 
  //LOG PRINT: &lt;{"name":"My first API blog post!","content_group_id":"351076997"},[Content-Type:"application/json"]&gt;
  </code></pre>
  
  <p><strong>With <code>.postForObject()</code>: 400 Bad Request</strong></p>
  
  <pre class="lang-java prettyprint-override"><code>BlogpostResponsePost answer = restTemplate.postForObject(
                                   "https://api.hubapi.com/content/api/v2/blog-posts?hapikey=***********",
                                    request, 
                                    BlogpostResponsePost.class);
  </code></pre>
  
  <p><strong>With <code>.exchange()</code>: 400 Bad Request</strong></p>
  
  <pre class="lang-java prettyprint-override"><code>BlogpostResponsePost answer = restTemplate.exchange(
                                    "https://api.hubapi.com/content/api/v2/blog-posts?hapikey=**********",
                                     HttpMethod.POST,
                                     request,
                                     BlogpostResponsePost.class);
  </code></pre>
  
  <hr>
  
  <h1>Using <a href="http://kong.github.io/unirest-java/" rel="nofollow noreferrer"><code>UniRest</code></a>: OK</h1>
  
  <pre class="lang-java prettyprint-override"><code>HttpResponse&lt;JsonNode&gt; resp = Unirest
          .post("https://api.hubapi.com/content/api/v2/blog-posts?hapikey=**********")
          .header("Content-Type", "application/json")
          .body(blogpostSendPost)
          .asJson();
  </code></pre>
  
  <hr>
  
  <p>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 <code>UniRest</code> lib.</p>
  
  <p>Thanks for your help guys !!</p>