简体   繁体   中英

curl command equivalent in java

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM