I have OkHttpClient setup and successfully sending a GET request to the server. And also I was able to send a POST request to the server with an empty body tag.
Now I am trying to send the following JSON object to the server.
{
"title": "Mr.",
"first_name":"Nifras",
"last_name": "",
"email": "nfil@gmail.com",
"contact_number": "75832366",
"billing_address": "",
"connected_via":"Application"
}
To do this, I'm trying to add the OkHttpClient library class RequestBody, but I cannot send the JSON object as the body of the HTTP POST request. In the next way, I will try to build the body and process the mail request.
OkHttpClient client = new OkHttpClient();
RequestBody body = new RequestBody() {
@Override
public MediaType contentType() {
return ApplicationContants.JSON;
}
@Override
public void writeTo(BufferedSink sink) throws IOException {
}
};
Request request = new Request.Builder()
.url(ApplicationContants.BASE_URL + ApplicationContants.CUSTOMER_URL)
.post(body)
.build();
How do I send a JSON object to the server through a POST request.
Thanks in advance.
source
share