Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set 'form-data' to jersey client post request

I am writing a rest client which consumes a POST restful sevice of server. Now the service expects 2 parameters as part of request in 'form-data'.

If you have across postman rest client, there we have an option to set the form-data, and give key-value pair parameters.

How can we send similar data in Javax rs rest client. Here is the code which I have.

import javax.ws.rs.client.Invocation.Builder;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.WebTarget;
...

Client client = ClientBuilder.newClient();
WebTarget target = client.target(url);
Builder basicRequest = target.request();        
Response response=basicRequest.post();

Now how to send 2 parameters namely 'fileName', 'fileVersion' with their values as part of form-data?

like image 858
Seeker Avatar asked Oct 27 '16 08:10

Seeker


1 Answers

After searching all over the internet, here is what worked for me

Client client = ClientBuilder.newClient();
WebTarget target = client.target(url);
Builder basicRequest = target.request();  

Form form = new Form();
form.param("filename", "file.csv")
form.param("version", "1.0");
Response response=basicRequest.post(Entity.form(form), Response.class);
like image 91
Seeker Avatar answered Sep 28 '22 14:09

Seeker