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());
    }
}
3
  • please check this thread stackoverflow.com/questions/9692166/… Commented Jul 19, 2021 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\"};) Commented Jul 19, 2021 at 14:52
  • see docs.oracle.com/en/java/javase/11/docs/api/java.base/java/nio/… Commented Jul 19, 2021 at 14:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.