Elad Benda Elad Benda - 2 months ago 22
HTTP Question

how to make javax.ws post request similar to postman request?

I try to send an http post request with postman and it works
(it works using

curl
as well)

I get a positive response

enter image description here

I try to do the same request in code using

import javax.ws.rs.client.WebTarget;



Map<String, String> formData = new HashMap();
formData.put("update_type", "config");
formData.put("role", role);
formData.put("name", configNameCamelCase);
formData.put("version", version);
formData.put("work_env", env);
formData.put("project", "waze-prod");
formData.put("provider", "gce");


try {
String a = commonClient.webTarget
.path("/tasks/update_version")
.request()
.header("Authorization", commonClient.authorizedRequestBuilder())
.accept(MediaType.APPLICATION_FORM_URLENCODED_TYPE)
.post(Entity.entity(formData, MediaType.APPLICATION_JSON))
.readEntity(String.class);
}


How should i change my code to make it identical to the postman request

Answer

Use Form and just add parameters with Form#param(key, value). Then use .post(Entity.form(form)).

Form form = new Form()
  .param("one", "two")
  .param("three", "four");

...post(Entity.form(form));