title: HttpsURLConnection - Send POST request
body: <p>I want to send a POST request to this particular API: <a href="https://developer.lufthansa.com/docs/read/api_basics/Getting_Started" rel="nofollow noreferrer">https://developer.lufthansa.com/docs/read/api_basics/Getting_Started</a> and I researched how to do that and tried everything but it simply doesn't work, I always get an HTTP 400 or an HTTP 401 error. Here's my code:</p>
  
  <pre><code>private void setAccessToken(String clientID, String clientSecret) {
      try {
          URL url = new URL(URL_BASE + "oauth/token");
          String params = "client_id=" + clientID + "&amp;client_secret=" + clientSecret + "&amp;grant_type=client_credentials";
          HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();
          connection.setRequestMethod("POST");
          connection.setDoInput(true);
          connection.setDoOutput(true);
          connection.connect();
          OutputStreamWriter osw = new OutputStreamWriter(connection.getOutputStream());
          osw.write(params);
          BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
          String line;
          while((line = br.readLine()) != null) {
              System.out.println(line);
          }
      } catch(IOException e) {
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>Kenta1561</p>