1

I would like some help to write in java the equivalent of the curl command below.

curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' --header  'cookie: [APP COOKIES];' -d 'sampleFile.json' 'https://url.com'
The Reedemed77
  • 419
  • 1
  • 6
  • 14
  • i doubt u can have one single command to do all this in hava . you may want to have a look at http://hc.apache.org/httpclient-3.x/tutorial.html – Rehan Azher Apr 18 '18 at 13:35

1 Answers1

0

With HttpClient of apache you can do it:

...
import org.apache.http.client.methods.HttpPost;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity
import org.apache.http.Consts;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.HttpEntity;
....

public String sendPost(String url, Map<String, String> postParams, 
        Map<String, String> header, String cookies) {

    HttpPost httpPost = new HttpPost(url);
    List<NameValuePair> formParams = new ArrayList<NameValuePair>();
    HttpClientBuilder clientBuilder = HttpClients.createDefault();
    CloseableHttpClient httpclient = clientBuilder.build();

    if (header != null) {
        Iterator<Entry<String, String>> itCabecera = header.entrySet().iterator();
        while (itCabecera.hasNext()) {
            Entry<String, String> entry = itCabecera.next();

            httpPost.addHeader(entry.getKey(), entry.getValue());
        }
    }

    httpPost.setHeader("Cookie", cookies);

    if (postParams != null) {
        Iterator<Entry<String, String>> itParms = postParams.entrySet().iterator();
        while (itParms.hasNext()) {
            Entry<String, String> entry = itParms.next();

            formParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
        }
    }

    UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(formParams, Consts.UTF_8);
    httpPost.setEntity(formEntity);

    CloseableHttpResponse httpResponse = httpclient.execute(httpPost);

    HttpEntity entity = httpResponse.getEntity();
    if (entity != null) {
        pageContent = EntityUtils.toString(entity);
    }

    return pageContent;
}

I hope this help you.

EDIT:

I add the way to send a file. I put in a code separate to not mix code (it is an approximation, based on this examples):

File file = new File("path/to/file");
String message = "This is a multipart post";
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

if (postParams != null) {
    Iterator<Entry<String, String>> itParms = postParams.entrySet().iterator();
    while (itParms.hasNext()) {
        Entry<String, String> entry = itParms.next();

        builder.addTextBody(entry.getKey(), entry.getValue(), ContentType.DEFAULT_BINARY);
    }
}

builder.addTextBody("text", message, ContentType.DEFAULT_BINARY);

HttpEntity entity = builder.build();
httpPost.setEntity(entity);
Alberto
  • 745
  • 1
  • 6
  • 25
  • This was very helpful. However here is the response I get **400 Bad request** Looks like there is some issue with the command... Just to make sure, find below what I'm doing: Map reqHeader = new HashMap<>(); reqHeader.put("--header ","'Content-Type: application/json' "); reqHeader.put("--header"," 'Accept: application/json' "); Map reqParams = new HashMap<>(); reqParams.put("-d '","path\sampleFile.json'"); httpPost.setHeader("--header Cookie:", cookies); Any pointer as to what I am doing wrong here? – The Reedemed77 Apr 18 '18 at 19:49
  • The correct way to send the parameters is: reqHeader.put("Content-Type","application/json"); reqHeader.put("Accept","application/json"); httpPost.setHeader("Cookie", cookies); Do you need send a file? If yes tell me to modify the example before – Alberto Apr 19 '18 at 06:20
  • Thanks a lot for your help. Yes I do need to send a json file. Here is the sample curl request again curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' --header 'cookie: [APP COOKIES];' -d 'sampleFile.json' 'https://url.com' – The Reedemed77 Apr 19 '18 at 13:55