HttpPost postRequest = new HttpPost("https://example.com/api/users"); StringEntity requestBody = new StringEntity("{\"name\":\"John\", \"email\":\"[email protected]\"}"); requestBody.setContentEncoding("UTF-8"); requestBody.setContentType("application/json"); postRequest.setEntity(requestBody);
HttpPost postRequest = new HttpPost("https://example.com/api/files/upload"); File fileToUpload = new File("C:/Users/username/Documents/myfile.txt"); MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.addPart("myFile", new FileBody(fileToUpload)); HttpEntity requestEntity = builder.build(); postRequest.setEntity(requestEntity);This example creates a POST request to the specified URL with a file attached as a multipart entity. The package library used in these examples is `org.apache.http.client.methods` which is a part of the Apache HttpClient library.