2

I wanna implement the HTTP post from curl.

curl $CURL_OPTS -X POST --data-binary {} -s -K <(cat <<< "-u micromdm:$API_TOKEN") "$SERVER_URL/$endpoint"

The curl command above sends empty binary data.

I know how to send string data.

Sending string data

try {
            CloseableHttpClient httpClient = null;
            try {
                httpClient = HttpClients.createDefault();
                HttpPost httpPost = new HttpPost(url);

                httpPost.addHeader("Content-Type", "application/json;charset=utf-8");
                httpPost.addHeader("Accept", "application/json;charset=utf-8");
                httpPost.addHeader("Authorization", "XXXXXXXX");

                StringEntity se = new StringEntity(jsonstring, "UTF-8");
                httpPost.setEntity(se);

                HttpResponse httpResponse = httpClient.execute(httpPost);
            catch () {
            }
catch () {
}

But how to send empty binary data?

Trying

                ByteArrayEntity byteArrayEntity = [];
                httpPost.setEntity(byteArrayEntity, ContentType.DEFAULT_BINARY);

It is not work.

I read some articles which are not properly solutions.

http-post-in-java-with-file-upload

how-to-get-raw-binary-data-from-a-post-request-processed-by-spring

uploading-binary-data-with-httppost

httpclient-post-http-request

There are many similar related articles, but I didn't find the solution. Can anyone give me the code sample for this? Thank you.

Nick Dong
  • 3,638
  • 8
  • 47
  • 84

2 Answers2

2

The code you provide is for json type, not binary type:

httpPost.addHeader("Content-Type", "application/json;charset=utf-8");

If you want an empty json request body, do:

StringEntity se = new StringEntity("{}", "UTF-8");

This the equivalent of

 --data-binary {}

"application/octet-stream" is a generic binary data type, meaning it can be used to represent any type of binary data. This MIME type is commonly used when the content type of the data is unknown or when the data being transferred does not fit any other specific MIME type. It is often used to transfer files or raw binary data.

For binary, use:

httpPost.addHeader("Content-Type", "application/octet-stream");

and initialize the entity with an empty inputStream:

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.impl.client.HttpClientBuilder;
....
public static void main(String[] args) throws IOException {
    
    String url = "https://example.com/api/binary-endpoint";
    
    // create HttpClient instance
    HttpClient httpClient = HttpClientBuilder.create().build();
    
    // create HttpPost instance with URL
    HttpPost httpPost = new HttpPost(url);
    
    // create empty HttpEntity with binary content type
    HttpEntity entity = new InputStreamEntity(IOUtils.toInputStream(""), ContentType.APPLICATION_OCTET_STREAM);
    
    // set entity to request
    httpPost.setEntity(entity);
    
    // send request
    httpClient.execute(httpPost);
}
Stempler
  • 1,309
  • 13
  • 25
  • The way using `{}`, `StringEntity se = new StringEntity("{}", "UTF-8"); ` works. But the way with InputStreamEntity get exception. `java.lang.IllegalArgumentException: Source input stream may not be null at org.apache.http.util.Args.notNull(Args.java:54) at org.apache.http.entity.InputStreamEntity.(InputStreamEntity.java:92) at org.apache.http.entity.InputStreamEntity.(InputStreamEntity.java:80)` – Nick Dong Mar 03 '23 at 02:25
  • Yes. I edited to code to initialize with empty input stream to use – Stempler Mar 05 '23 at 15:13
  • The former method with "{}" works. But the method `new InputStreamEntity(IOUtils.toInputStream(""), ContentType.APPLICATION_OCTET_STREAM)` does not work. – Nick Dong Mar 24 '23 at 07:03
0
byte[] emptyData = new byte[0];

HttpPost httpPost = new HttpPost("http://example.com/upload");

httpPost.setEntity(new ByteArrayEntity(emptyData));

HttpClient httpClient = new DefaultHttpClient();
HttpResponse httpResponse = httpClient.execute(httpPost);
Stempler
  • 1,309
  • 13
  • 25
  • Next, the setEntity() method is called on the HttpPost object, and an instance of ByteArrayEntity is created with the empty byte array as its argument. The ByteArrayEntity is used to represent the binary data being sent in the HttpPost request. – Majid Asadi Feb 26 '23 at 06:35
  • Thank you. What does the URL `"http://example.com/upload"` mean? I just wanna implement the `--data-binary {}`. – Nick Dong Feb 26 '23 at 06:46
  • this is your url => in line HttpPost httpPost = new HttpPost(url); – Majid Asadi Feb 26 '23 at 07:07
  • I am trying to implement a curl sample request. It supplies a request with empty binary data. And the interface, which the curl command requests, does not need any file uploads. Is it ok with an empty argument to HttpPost? – Nick Dong Feb 26 '23 at 08:36