POST request with OkHttp in JAVA

My question is like this, I use the postman to simulate these requests, then when I choose there to show the code as it would be in java shows this code below, but ta full of" strange characters " like this WebKitFormBoundary7MA4YWxkTrZu0gW, and if I remove them does not work the request when I test here. Would anyone know what these codes are all about, and how would the correct way I would send a post, with the data I want,a JSON for example, without having to add these codes, or at least some class or method that automatically generates these codes when submitting the request?

OkHttpClient client = new OkHttpClient();

MediaType mediaType = MediaType.parse("multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
RequestBody body = RequestBody.create(mediaType, "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"grant_type\"\r\n\r\npassword\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"client_id\"\r\n\r\ntestclient\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"client_secret\"\r\n\r\ntestpass\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"username\"\r\n\r\nchaves\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"password\"\r\n\r\ndiscovoador\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--");
Request request = new Request.Builder()
  .url("http://api.sualoja.com.br/oauth")
  .post(body)
  .addHeader("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW")
  .addHeader("cache-control", "no-cache")
  .addHeader("postman-token", "ae657120-da97-0123-ed66-bea56efdd3a8")
  .build();

Response response = client.newCall(request).execute();
Author: TheJ, 2017-10-02

1 answers

Your request has this from here:

multipart/form-data

That is, it is a multipart request, a request that as the name says is made up of multiple parts. These parts are separated from each other by a separator, which is the boundary. This boundary was specified by the browser according to RFC 2046 as being ----WebKitFormBoundary7MA4YWxkTrZu0gW. This string has this bizarre format because it can't be anything that has any chance of appearing naturally in the data to be send.

Let's see the contents of your request:

------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name=\"grant_type\"

password
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name=\"client_id\"

testclient
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name=\"client_secret\"

testpass
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name=\"username\"

chaves
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name=\"password\"

discovoador
------WebKitFormBoundary7MA4YWxkTrZu0gW--

There are 5 parts here. Each part contains a Content-Disposition header and a body that is a word. The headers and body of each part are separated by a blank line.

I think what you wanted is this:

String json = ""
        + "{"
            + "\"grant_type\": \"password\","
            + "\"client_id\": \"testclient\","
            + "\"client_secret\": \"testpass\","
            + "\"username\": \"chaves\","
            + "\"password\": \"discovoador\""
        + "}";

OkHttpClient client = new OkHttpClient();

MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
RequestBody body = RequestBody.create(mediaType, json);
Request request = new Request.Builder()
  .url("http://api.sualoja.com.br/oauth")
  .post(body)
  .addHeader("cache-control", "no-cache")
  .addHeader("postman-token", "ae657120-da97-0123-ed66-bea56efdd3a8")
  .build();

Response response = client.newCall(request).execute();
 1
Author: Victor Stafusa, 2017-10-02 18:22:07