0

I am developing a native Java REST client to submit a POST request to the server with the below Post info and its payload. I am using Apache's HttpClient and HttpPost.

The challenge I am facing is how to define and set the boundary and pass the JSON object in the post body. When I test the below post request with a rest client tool(like Postman or Advanced REST client), it works. However I need to implement it in my native Java code. How do I pass the below data, in particularly the information starting from MYBOUNDARY to --MYBOUNDARY. Do I need to pass newline/blank lines and do some kind of encoding?

Below is the POST request and its payload I need to send.

POST /savedata/saveimage/imageid HTTP/1.1
Host: mywebsite.com
Authorization: Bearer T1touCsbcNNrQpMdJjqspLCwhbiQ
Accept: application/json, text/plain, */*
Content-Type: multipart/form-data; boundary=MYBOUNDARY
Cache-Control: no-cache

MYBOUNDARY
Content-Disposition: form-data; name="entity_priority"

{"Name":"Jack","Date":"2017-01-25T11:06:33.834Z","notes":{"records":[{"Text":"123456789","Type":"None","Attachments":{"records":[]}}]},"CreatedBy":{"user":"2020"}}
--MYBOUNDARY

1 Answers1

0

If you're going to write your own implementation, you should start by reading the specification on how multipart/* works, e.g. RFC1341(MIME) : 7 The Multipart content type, or HTML 4.01 Specification - multipart/form-data.

Your problem is that with a boundary value of MYBOUNDARY, the actual boundary is --MYBOUNDARY, and the terminator is --MYBOUNDARY--.

Andreas
  • 154,647
  • 11
  • 152
  • 247
  • Thanks Andreas for your suggestions. Can you also help me know if we have any implementations available by Apache(or any other open source) that solves the above problem. – Subhabrata Sen Jan 28 '17 at 06:00