Reputation: 1
I haven't been able to hit an API successfully. I need to pass three values in the body of the POST request and a header: "Content-Type":"application/x-www-form-urlencoded"
.
I'm getting a 400 status, along with this:
Content-Type=[application/json;charset=UTF-8]
, can someone point me in the right direction here?
Client client = ClientBuilder.newClient();
WebTarget webTarget = client.target(REST_URI).path("dummy/dummy"); // REST_URI is a constant containing the URL
// Create body content
String json = Json.createObjectBuilder()
.add("grant_type", "password")
.add("username", USERNAME) // USERNAME/PASSWORD are constants
.add("password", PASSWORD)
.build()
.toString();
Response response = webTarget.request(MediaType.APPLICATION_FORM_URLENCODED)
//.header("Content-Type", "application/x-www-form-urlencoded")
.accept(MediaType.APPLICATION_FORM_URLENCODED)
.post(Entity.entity(json, MediaType.APPLICATION_JSON));
Upvotes: 0
Views: 3309
Reputation: 1466
You can try with the following code. You have to use the method .type() where you have to mention content-type.
Response response = webTarget.resource(URL)
.accept(MediaType.APPLICATION_JSON)
.type(MediaType.APPLICATION_JSON)
.post(Entity.entity(json, MediaType.APPLICATION_JSON));
You can also refer about how to make client from the following link.
https://howtodoinjava.com/jersey/jersey-restful-client-examples/#post
Upvotes: 1