Reputation: 265
I am using CodenameOne to send a POST request to a REST API. It works perfectly with the GET because I do not have to pass a BODY with the message. Please can someone tell me how to pass a BODY with my post message?
Here is the code I am using to connect ...
try {
ConnectionRequest connReq = new ConnectionRequest();
connReq.setPost(true);
connReq.addRequestHeader("Authorization", "54321);
connReq.addRequestHeader("client_id","12345");
connReq.addRequestHeader("Content-Type","application/json");
connReq.setUrl("https://myapi.com/connect");
connReq.setHttpMethod("POST");
NetworkManager.getInstance().addToQueueAndWait(connReq);
Map<String,Object> result = new JSONParser().parseJSON(new InputStreamReader(new ByteArrayInputStream(connReq.getResponseData()), "UTF-8"));
return result;
}
catch(Exception err) {
System.err.println(err);
return null;
}
I have found some documentation here - but I can't quite understand what they are telling me ...
Thanks
Upvotes: 1
Views: 722
Reputation: 52770
If you want a standard "FORM" style post which is what you would get if you have fields in an HTML form just use addArgument
as you would with GET
. It will work with POST
just fine.
If you want to "hardcode" your body e.g. some webservices expect JSON to be the body of the request just write it into the output stream in the buildRequestBody
method you found.
Upvotes: 2