Paul N
Paul N

Reputation: 45

Java cURL alternative with HttpURLConnection

I was asked to port a PHP module I was writing to Java. I was previously using PHP's native cURL library, now trying to achieve the same action with HttpURLConnection.

Here's the call I want to do with cURL:

curl -u 'ExactID:Password' \
     -H 'Content-Type: application/json; charset=UTF-8' \
     -H 'Accept: application/json' \
     -d '{
 "transaction_type":"00",
 "amount":"15.75",
 "cardholder_name":"PaulTest",
 "transarmor_token":"3000",
 "credit_card_type":"Visa",
 "cc_expiry":"0016",
}' \
https://api.demo.globalgatewaye4.firstdata.com/transaction/v11

Here's what I have in Java, which returns a HTTP 400 error. Any ideas?

public static void main(String[] args) {     
    URL url = new URL("https://api.demo.globalgatewaye4.firstdata.com/transaction/v11");
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setDoOutput(true);
    conn.setDoInput(true);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
    conn.setRequestProperty("Accept", "application/json");

    String userpass = "ExactID" + ":" + "Password";
    String basicAuth = "Basic " + new String(new Base64().encode(userpass.getBytes()));
    conn.setRequestProperty ("Authorization", basicAuth);

    JSONObject obj = new JSONObject();
    obj.put("transaction_type", "00");
    obj.put("amount", "10");
    obj.put("cardholder_name", "PaulTest");
    obj.put("transarmor_token", "3000");
    obj.put("cc_expiry", "0016");
    obj.put("credit_card_type", "Visa");

    String input = obj.toString();
    System.out.println(input);

    OutputStream os = conn.getOutputStream();
    os.write(input.getBytes());
    os.flush();

    if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {

    throw new RuntimeException("Failed : HTTP error code : "+ conn.getResponseCode() + conn.getResponseMessage());

}

Upvotes: 2

Views: 4124

Answers (2)

Paul N
Paul N

Reputation: 45

After tinkering around, I made two mistakes:

For this POST method, basic authentication was not required. The user & pw goes into the JSON body along with the other parameters.

Also, my "transarmor_token" field needed to be 16 digits.

Conclusion: HttpURLConnection is a great cURL alternative. Forget about using the curl-java binding.

Thanks!

Upvotes: 0

gerrytan
gerrytan

Reputation: 41123

One ambiguity in your java code is on string to byte array encoding. By default java will use your default platform encoding, but it's a good practice to express it explicitly because it often lead to hard to track bug

String basicAuth = "Basic " + new String(new Base64().encode(userpass.getBytes("ISO-8859-1")));

To be sure also check the encoded base 64 value generated by java on curl by using

-H 'Authorization: Basic ....`

Instead of -u

Also I'd try to cast the created URLConnection to HttpsURLConnection. Thay may/not make difference

HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();

Upvotes: 1

Related Questions