title: HTTP post/API request works when sent from CURL on bash, fails from Apache http
body: <p>I'm trying to use apache http components to interface with the Spotify api. The request I'm trying to send is detailed <a href="https://developer.spotify.com/web-api/authorization-guide/#client_credentials_flow" rel="nofollow noreferrer">here</a> under #1. When I send this request from bash using curl </p>
  
  <p><code>curl -H "Authorization: Basic SOMETOKEN" -d grant_type=client_credentials https://accounts.spotify.com/api/token</code> </p>
  
  <p>I get back a token like the website describes</p>
  
  <p>However the following java code, which as far as I can tell executes the same request, gives back a 400 error</p>
  
  <p>Code</p>
  
  <pre><code>    String encoded = "SOMETOKEN";
      CloseableHttpResponse response = null;
      try {
          CloseableHttpClient client = HttpClients.createDefault();
          URI auth = new URIBuilder()
                  .setScheme("https")
                  .setHost("accounts.spotify.com")
                  .setPath("/api/token")
                  .setParameter("grant_type", "client_credentials")
                  .build();
  
          HttpPost post = new HttpPost(auth);
          Header header = new BasicHeader("Authorization", "Basic " + encoded);
          post.setHeader(header);
          try {
              response = client.execute(post);
              response.getEntity().writeTo(System.out);
          }
          finally {
              response.close();
          }
      } catch (Exception e) {
          e.printStackTrace();
      }
  </code></pre>
  
  <p>Error</p>
  
  <blockquote>
    <p>{"error":"server_error","error_description":"Unexpected status: 400"}</p>
  </blockquote>
  
  <p>The URI that the code prints is looks like this</p>
  
  <blockquote>
    <p><a href="https://accounts.spotify.com/api/token?grant_type=client_credentials" rel="nofollow noreferrer">https://accounts.spotify.com/api/token?grant_type=client_credentials</a></p>
  </blockquote>
  
  <p>And the header looks like this</p>
  
  <blockquote>
    <p>Authorization: Basic SOMETOKEN</p>
  </blockquote>
  
  <p>Am I not constructing the request correctly? Or am I missing something else?</p>