How to add bellow curl comment to the jersey client.
curl -X POST --user 'gigy:secret' -d 'grant_type=password&username=peter@example.com&password=password' http://localhost:8000/gigy/oauth/token
I tried to like bellow. but I don't know how to add other things.
Client client = Client.create();
WebResource webResource = client.resource("http://localhost:8000/gigy/oauth/token");
--user 'gigy:secret'
You need Basic Authentication. Basically You need to set the Authorization
header with the value Basic base64("gigy:secret")
, where base64
is whatever you use to convert the string "user:password"
to its Base 64 counterpart. You can set headers on the WebResource
calling one it's header
method.
-d 'grant_type=password&username=peter@example.com&password=password'
These are application/x-www-form-urlencoded
parameters. This is what you will need to send as the entity body of the request. With Jersey you can use the com.sun.jersey.api.representation.Form
class. Once you create it, just add key value/pairs to it like key=grant_type and value=password. All the pairs split by &
.
Implicit Media type.
When you don't set the Content-Type
header in your cURL request, a POST will default to application/x-www-form-urlencoded
. You need to set this using the type(MediaType)
function after you call header
. Use MediaType.APPLICATION_FORM_URLENCODED_TYPE
.
-X POST
Now you need to send the request. Just call post
after you call type
, with the following arguments .post(ClientResponse.class, yourForm)
. This will return a ClientResponse
.