POST Request with OkHttp in JAVA

1

My question is, I'm using postman to simulate these requests, so when I choose there to show the code as it would be in java it shows this code below, but it's full of "strange characters" like this WebKitFormBoundary7MA4YWxkTrZu0gW and if I remove them the requisition does not work when I test here. Someone would know what these codes are about, and how would it be the correct way to send a post, with the data I want, a JSON for example, without having to add those codes, or at least some class or method that generates automatically when sending 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();
    
asked by anonymous 02.10.2017 / 19:29

1 answer

1

Your request has it 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 ----WebKitFormBoundary7MA4YWxkTrZu0gW . This string has this bizarre format because it can not be anything that has any chance to appear naturally in the data to be sent.

Let's see the content 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();
    
02.10.2017 / 19:46