Monday 4 November 2019

Send Http Post from a native Java Rest client

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

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...