Seeker
Seeker

Reputation: 2475

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?

Upvotes: 6

Views: 9098

Answers (1)

Seeker
Seeker

Reputation: 2475

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);

Upvotes: 14

Related Questions