0

So I've looked into many solutions here , but couldn't find this...

Say I have json file "myFile.json" - it hold the body of the request Say I have this path as destination URL : "http://1.1.1.7:80/myService/publish"

All I want to do is send this file to the destination.

Here is a code I'm using (found it here in one of the threads)

public class HttpClientExample {
    public static void main(String[] args) throws Exception {
        String payload = "data={"myFile.json"};
        StringEntity entity = new StringEntity(payload,
                ContentType.APPLICATION_FORM_URLENCODED);

        HttpClient httpClient = HttpClientBuilder.create().build();
        HttpPost request = new HttpPost("http://1.1.1.7:80/myService/publis");
        request.setEntity(entity);

        HttpResponse response = httpClient.execute(request);
        System.out.println(response.getStatusLine().getStatusCode());
    }
}
andrewJames
  • 19,570
  • 8
  • 19
  • 51
Spartacus
  • 378
  • 1
  • 4
  • 12
  • please check this thread https://stackoverflow.com/questions/9692166/http-post-in-java-with-file-upload – pratap Jul 19 '21 at 14:20
  • You should read the contents of the file -- what you've got here is just sending the text value of the filename, except for the syntax error where you didn't escape your double-quotes around the filename (`data={\"myFile.json\"};`) – Gus Jul 19 '21 at 14:52
  • see https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/nio/file/Files.html#readString(java.nio.file.Path) – Gus Jul 19 '21 at 14:56

0 Answers0