4

Here is my curl command:

curl https://login.xyz.com/v1/oauth/token -H "Accept:
application/json" --data 'client_id=client_id' --data
'client_secret=client_secret' --data 'redirect_uri=redirect_uri'
--data 'code=code'

I'm trying to post that in java. Here is what i was trying to do:

String resourceUrl = "https://login.xyz.com/v1/oauth/token?client_id=<client.id>&client_secret=<client.secret>&redirect_uri=https://login.xyz.com/user/login&code=<code>";
HttpURLConnection httpcon = (HttpURLConnection) ((new URL(resourceUrl).openConnection()));
httpcon.setDoOutput(true);
httpcon.setRequestProperty("Content-Type", "application/json");
httpcon.setRequestProperty("Accept", "application/json");
httpcon.setRequestMethod("POST");
httpcon.connect();      
System.out.println(httpcon.getHeaderField(0));

But I'm getting HTTP/1.1 500 Internal Server Error

2 Answers 2

1

I didn't test but just by looking at the documentation and your source code, I can see some differences between your curl command and the Java implementation:

Curl:

  • Performs a POST
  • Content-Type is application/x-www-form-urlencoded

Curl manpage:

-d, --data

(HTTP) Sends the specified data in a POST request to the HTTP server, in the same way that a browser does when a user has filled in an HTML form and presses the submit button. This will cause curl to pass the data to the server using the content-type application/x-www-form-urlencoded. Compare to -F, --form.

See also: How are parameters sent in an HTTP POST request?

Java implementation:

  • Performs a POST but URL is GET-alike (you set the request method to POST but you're passing the parameters in the URL query string)
  • Content-Type is application/json

I hope this helps.

Sign up to request clarification or add additional context in comments.

3 Comments

for POST required: -X/--request <command>, -d - just a key for specify data values
it seems you right, -X just for situations when you want to send POST without any data
1
public class CURLTest {
    public void main(String[] args) throws IOException {
        sendData();
    }

    public String sendData() throws IOException {
        // curl_init and url


        URL url = new URL( "Put the Request here");
        HttpURLConnection con = (HttpURLConnection) url.openConnection();

        // CURLOPT_POST
        con.setRequestMethod("POST");

        // CURLOPT_FOLLOWLOCATION
        con.setInstanceFollowRedirects(true);

        String postData = "my_data_for_posting";
        con.setRequestProperty("Content-length",
            String.valueOf(postData.length()));

        con.setDoOutput(true);
        con.setDoInput(true);

        DataOutputStream output = new DataOutputStream(con.getOutputStream());
        output.writeBytes(postData);
        output.close();

        // "Post data send ... waiting for reply");
        int code = con.getResponseCode(); // 200 = HTTP_OK
        System.out.println("Response    (Code):" + code);
        System.out.println("Response (Message):" + con.getResponseMessage());

        // read the response
        DataInputStream input = new DataInputStream(con.getInputStream());
        int c;
        StringBuilder resultBuf = new StringBuilder();
        while ((c = input.read()) != -1) {
            resultBuf.append((char) c);
        }
        input.close();

        return resultBuf.toString();
    }
}

Here is an example how I would do it

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.