Making an api request with auth tokens through Java
<p>I'm pretty new to software development and I'm working on a project that makes API calls to a transaction server with a bearer token. </p>

<p>I've figured out how to call the API through curl via command line. It looks something like this (it is through http rather than https because I am using their test server which only supports http requests)</p>

<pre><code>curl http://api.testwebsite.com/transactions/ \
-H "Authorization: Bearer &lt;TOKEN ID&gt;" \
-d amount=500 \
-d currency="USD" \
-d card_number="&lt;card number&gt;" \
-d card_exp_month=&lt;exp month&gt; \
-d card_exp_year=&lt;exp year&gt; \
-d card_cvv="&lt;card's ccv&gt;"
</code></pre>

<p>Now I'm writing a program in Java (has to be Java) using only native Java libraries (no 3rd party packages like Spring or Apache) and I'm trying to call the API through HttpUrlConnection. </p>

<pre><code>import java.io.BufferedReader; 
import java.io.InputStreamReader;
import java.io.DataOutputStream; 
import java.net.URL;             
import javax.net.ssl.HttpsURLConnection;
import java.net.HttpURLConnection;

public class TestHttpsPost {

public static void main(String[] args) throws Exception {

    // Uncomment one of these next two lines:
    URL url = new URL("http://api.testwebsite.com/transactions");
    //URL url = new URL("https://httpbin.org/post"); // good for testing

    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("POST");
    connection.setDoInput(true);
    connection.setDoOutput(true);

    // Change this to a valid token:
    connection.setRequestProperty("Authorization", "Bearer &lt;TOKEN ID&gt;");
    /*

    connection.setRequestProperty("amount", "500");
    connection.setRequestProperty("currency", "USD");
    connection.setRequestProperty("card_number", "&lt;CARD_NUMBER&gt;");
    connection.setRequestProperty("card_exp_month", "&lt;EXP_MONTH&gt;");
    connection.setRequestProperty("card_exp_year", "&lt;EXP_YEAR");
    connection.setRequestProperty("card_cvv", "&lt;CCV&gt;");
    */




    String jsonData1 = "amount=500";
    String jsonData2 = "currency=\"USD\"";
    String jsonData3 = "card_number=\"11111111111111111\"";
    String jsonData4 = "card_exp_month=11";
    String jsonData5 = "card_exp_year=2011";
    String jsonData6 = "card_cvv=\"123\"";




    try {
        // Post the data:
        DataOutputStream output = new DataOutputStream(connection.getOutputStream());
        output.writeBytes(jsonData1);
        output.writeBytes(jsonData2);
        output.writeBytes(jsonData3);
        output.writeBytes(jsonData4);
        output.writeBytes(jsonData5);
        output.writeBytes(jsonData6);

        output.close();

        // Read the response:
        BufferedReader reader = new BufferedReader(new InputStreamReader(
          connection.getInputStream()));
        String line;
        while ((line = reader.readLine()) != null) {
            System.out.println(line);
        }
        reader.close();
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }

    System.out.println("Response code:" + connection.getResponseCode());
    System.out.println("Response message:" + connection.getResponseMessage());
}
}
</code></pre>

<p>But I keep getting </p>

<pre><code>Server returned HTTP response code: 400 for URL: http://api.testwebsite.com/transactions
Response code:400
Response message:Bad Request
</code></pre>

<p>I followed <a href="https://stackoverflow.com/questions/31627733/transforming-a-paypal-curl-request-to-an-http-request-in-java">Transforming a PayPal curl request to an http request in Java</a>, but the fields for the paypal api are pretty different and I'm not sure if I messed anything up.</p>

<p>Thanks in advance!</p>