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?
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With