1

I have this code below that trying to post JSON data after successfully authenticate through Oauth2.0, the code is like this:

try {            
    URL url = new URL ("https://test.api.neteller.com/v1/oauth2/token?grant_type=client_credentials");
    String test = "QUFBQlNhVlFoUjhlTVhPQjowLm9aV3g2Rkhwd1dIQUZnXzRaMDZwOUoyRWtzaXVHMHEzd2xrVFF4MkRVM28uN0FzUDE4cmZXU0dxVDdqWE1NUGhzSFY4cHU0";
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("POST");
    connection.setDoOutput(true);
    connection.setRequestProperty  ("Authorization", "Basic " + test);
    String accessToken = "";
    [... get the access token ...]

    String urlnew = "https://test.api.neteller.com/v1/transferIn";
    URL obj = new URL(urlnew);
    HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

    //add request header
    con.setRequestMethod("POST");
    con.setDoOutput(true);          
    con.setRequestProperty  ("Authorization", "Bearer " + accessToken);
    con.setRequestProperty("Content-Type", "application/json");

    // Send post request            
    JSONObject obj2 = new JSONObject();
    obj2.put("paymentMethod", "");
    obj2.put("type", "neteller");
    obj2.put("value", "[email protected]");
    obj2.put("transaction", "");
    obj2.put("merchantRefId", "20140203122501");
    obj2.put("amount",  (5000));
    obj2.put("currency", "USD");
    obj2.put("verificationCode", "411392");
    System.out.print(obj2);             

    int responseCode = con.getResponseCode();
    System.out.println("Response Code : " + responseCode);

    [... parse response ...]
} catch(Exception e) {
    e.printStackTrace();
}

I get this error when I run the code above:

java.io.IOException: Server returned HTTP response code: 400 for URL: https://test.api.neteller.com/v1/transferIn
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection$6.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.net.www.protocol.http.HttpURLConnection.getChainedException(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
    at example.demo.main(demo.java:71)
Caused by: java.io.IOException: Server returned HTTP response code: 400 for URL: https://test.api.neteller.com/v1/transferIn
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at java.net.HttpURLConnection.getResponseCode(Unknown Source)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(Unknown Source)
    at example.demo.main(demo.java:65)

From the error status 400 I know that the post JSON data is invalid. I suspect maybe in the structure because according to the documentation it should be like this:

{
    "paymentMethod": {
        "type": "neteller",
        "value": "[email protected]"
    },
    "transaction": {
        "merchantRefId": "20140203122501",
        "amount": 5000,
        "currency": "USD"
    },
    "verificationCode": "234124"
}

So how do I change the JSON parameter so that it could follow the structure in the documentation?

3
  • paymentMethod and transaction should be JSONObjects themselves.
    – guido
    Nov 18, 2014 at 14:33
  • also you are not writing your json anywhere else than the console....
    – guido
    Nov 18, 2014 at 14:37
  • What do you mean they should be JSONObject themselves? also where else should i write them? thanks for your reply
    – StrikeNeo
    Nov 18, 2014 at 15:15

1 Answer 1

2

For the required data scheme, like this:

JSONObject obj2 = new JSONObject();
JSONObject paymentMethod = new JSONObject();
paymentMethod.put("type", "neteller");
paymentMethod.put("value", "[email protected]");
obj2.put("paymentMethod", paymentMethod);
JSONObject transaction = new JSONObject();
transaction.put("merchantRefId", "20140203122501");
transaction.put("amount",  (5000));
transaction.put("currency", "USD");
obj2.put("transaction", transaction);
obj2.put("verificationCode", "411392");

For writing in the outputStream of the connection (the payload of the POST):

BufferedWriter out = 
    new BufferedWriter(new OutputStreamWriter(con.getOutputStream()));
out.write(obj2.toString());
out.close();

the above, before reading the responseCode.

1
  • OMG it works! Thanks so much, u save me a lot of trouble, THANKS again
    – StrikeNeo
    Nov 18, 2014 at 16:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.