bobby
bobby

Reputation: 183

How to send x-www-form request on java?

enter image description here

Above is what im trying to send

In java this is what I have

        RequestBody formBody = new FormBody.Builder()
                .add("param1", "abc")
                .add("param2", "abc")
                .add("param3", "abc")
                .build();

        Request request = new Request.Builder()
                .url("http://localhost:3001/addsomething")
                .post(formBody)
                .build();

doesn't seem to work. I have OkHttpClient but I'm not sure how to use it to send the above result

Upvotes: 1

Views: 77

Answers (1)

spinyBabbler
spinyBabbler

Reputation: 407

What are you confused about? I'm a little unsure. I did a little research but it seems the only thing you're missing is a client, and then sending the request you've created and receive a response back.

To create a client, look at the most updated documentation on OkHttpClient, but this is what I found:

OkHttpClient client = new OkHttpClient();

And then send your request using that client using:

Response response = client.newCall(request).execute();

Then you can proceed to do something with that response.

All you have to understand is that you're creating a request (essentially asking the server for some information). Depending on your request, you'll get a response back (as in above), which you can then use to get whatever you're looking for.

Upvotes: 1

Related Questions