简体   繁体   中英

Java equivalent to curl command

I am currently attempting to figure out the Java equivalent to this curl command:

curl -X POST -u username:password -H "X-Atlassian-Token: no-check" http://example.com/rest/api/1.0/projects/STASH/avatar.png -F avatar=@avatar.png

Any help would be appreciated.

So far, I've had success using the Apache HTTP library. Below is an example of a POST request that I have used successfully. However, this example is the equivalent of this curl command:

curl -X POST -u username:password -H "Content-type: application/json" --data '{\"name\":\"projectName\", \"key\":\"KEY\", \"description\":\"good?\"}' "http://localhost:7990/rest/api/1.0/projects"

and the java equivalent:

// initialize connection
CloseableHttpClient client = HttpClients.createDefault();
HttpPost post = new HttpPost("http://localhost:7990/rest/api/1.0/projects")
try
{
    // create a request using the input
    StringEntity request = new StringEntity("{\"name\":\"projectName\", \"key\":\"KEY\", \"description\":\"good?\"}",ContentType.APPLICATION_JSON);
    post.setEntity(request);
    // add credentials to the header in order to get authorization
    String credentials = username + ":" + password
    byte[] encodedCredentials = Base64.encodeBase64(credentials.getBytes("UTF-8"));
    String header = "Basic " + new String(encodedCredentials);
    post.addHeader("Authorization",header);
    // execute the request using the POST method
    client.execute(post);
}
catch(Exception e)
{
    // nada
}
finally
{
    // close the connection
    post.releaseConnection();
}

This is what I've come up with for mimicking the curl command I first mentioned:

// initialize connection
CloseableHttpClient client = HttpClients.createDefault();
HttpPost post = new HttpPost(HOST_URL + uri);
try
{
    // create a request using the input
    File avatar = new File("avatar.png")
    FileBody uploadFilePart = new FileBody(avatar);
    MultipartEntity request = new MultipartEntity();
    request.addPart("upload-file", uploadFilePart);
    post.setEntity(request);
    // add credentials to the header in order to get authorization
    byte[] encodedCredentials = Base64.encodeBase64(credentials.getBytes("UTF-8"));
    String header = "Basic " + new String(encodedCredentials);
    post.addHeader("Authorization",header);
    // add the other header peice
    post.addHeader("X-Atlassian-Token","no-check");
    // execute the request
    client.execute(post);
}
catch(Exception e)
{
    // nada
}
finally
{
    // close the connection
    post.releaseConnection();
}

I think its just the file uploading part that's tripping me up. I know the original curl request works, I've run it in git bash successfully.

While searching for the proper way to upload files I've come across examples that use different versions of multi-part data, such as the MultipartEntityBuilder or MultipartRequestEntity. But so far I haven't had success with any of them (That's not to say they were wrong, I just don't know what I'm doing).

You can make use of java.net.URL and/or java.net.URLConnection.

URL url = new URL("http://stackoverflow.com");

try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"))) {
    for (String line; (line = reader.readLine()) != null;) {
        System.out.println(line);
    }
}

Also see the Oracle's simple tutorial on the subject. It's however a bit verbose. To end up with less verbose code, you may want to consider Apache HttpClient instead.

See

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