POST request with Okhttp in JAVA

Asked

Viewed 981 times

1

My question is like this, I’m using 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 full of "strange characters" like this WebKitFormBoundary7MA4YWxkTrZu0gW, And if I remove them the requisition doesn’t work when I test here. Would anyone know what these codes are about, and how it would be the right way for me to 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 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();
  • 1

    What are you asking? What is your question?

  • was bad, gave enter unintentionally here. If you have not well explained mt let me know to edit. And obr for editing my question.

1 answer

1


Your requisition 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. That Boundary was specified by the browser according to the RFC 2046 as being ----WebKitFormBoundary7MA4YWxkTrZu0gW. This string has this bizarre format because it cannot be anything that has any chance of appearing naturally in the data to be sent.

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 header Content-Disposition 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();
  • So inevitably, when I go to do a POST, msm that is a JSON for example, I need to follow exactly this logic? And "adapt" the data contained in my json to that format?

  • @Thej No. Only if you want to do a Multipart post. However, I think Multipart is not what you want.

  • My goal is for example to take a login and a password, and send it to this Postman API, so that it checks in the database if the data exists and if they are correct. As if it were a login system but done in java SE

  • @Thej I edited the answer. See if the code at the end works.

  • It didn’t work. It did Invalid request.

  • @Thej I tried to give one more change based in this reply by Soen. I took the explicit definition from the header Content-Type because the MediaType already implies that.

  • It worked, you just changed the field grant-type for grant-name. I fixed it and it worked.Obg.

  • I need to open another question to know how I get the data that is returned to me?

  • @Thej Ok, even having already been solved, I arranged the answer to put grant_type For the record, I’m sorry about the mistake. As for the data returned, I suggest opening another question, even because this is another question and it may be that another person knows how to answer and also questions that keep changing a lot what is being asked not without well seen around here, so you better not take any chances with it.

  • Quiet, and I figured out here how to catch kk It’s pretty simple in vdd used response.body().string(). Thank you most for your help.

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.