Shruti sharma
Shruti sharma

Reputation: 211

415 error while calling post API from jersey client

I have below API which returns back the access_token.

POST https://idcs-xxxxxxxxxbf08128c3d93a19c.identity.c9dev2.oc9qadev.com/oauth2/v1/token

in header content-type is application/x-www-form-urlencoded. also in body it contains below parameter.

enter image description here

I send user name and password and it is secured with basic authentication. It provides access_token when I call from postman. also it provides output when I consume using HttpUrlConnection

          url = new URL(tokenURL);
        connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        connection.setRequestProperty("Authorization", auth);
        connection.setRequestProperty("Accept", "application/json");
        OutputStream os = connection.getOutputStream();
        OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
        osw.write("grant_type=client_credentials&scope=" + scope);

The above code is working properly. But when I use jersey it gives 415 error. I am using below code.

String user="idcs-oda-zzzxxxxxf93560b94eb8a2e2a4c9aac9a3ff-t0_APPID";       
    String password="xxxxxxx-6f71-4af2-b5cc-9110890d1456";
    String scope = "https://idcs-oda-xxxxxxxxxxxxxxxxe2a4c9aac9a3ff-t0.data.digitalassistant.oci.oc-test.com/api/v1";
    String tokenURL = "https://idcs-xxxxxxxxxxxxxxxx28c3d93a19c.identity.c9dev2.oc9qadev.com/oauth2/v1/token";
    HttpAuthenticationFeature feature= HttpAuthenticationFeature
                                .basicBuilder()
                                .nonPreemptive()
                                .credentials(user,password)
                                .build();
    ClientConfig clientConfig = new ClientConfig();
    clientConfig.register(feature);
    
    Client client = ClientBuilder.newClient(clientConfig);
    WebTarget webTarget= client.target(tokenURL);
    PostDetails post= new PostDetails("client_credentials",scope);  //Bean class to assign body parameter
    Response response= webTarget.request()
                       .header("Content-Type", "application/x-www-form-urlencoded")
                       .post(Entity.json(post)); 
    System.out.println(response);

Can somebody tell me what mistake I am doing in Response line.

Upvotes: 2

Views: 537

Answers (2)

Jure Skelin
Jure Skelin

Reputation: 91

What you need is:

Response response= webTarget.request()
    .accept("application/json") // Accept field from header of request
    .header("Content-Type", "application/x-www-form-urlencoded") //manually set content-tyoe
    .post(Entity.entity(input, MediaType.TEXT_PLAIN)); // request body

The best way to see what is Jersey actually is sending is to register logger, and log network. For example:

clientConfig.register(
    new LoggingFeature(
            new Slf4jLogger(this.getClass().getName(), null)));

where Slf4jLogger is from org.apache.cxf:cxf-core.

Upvotes: 2

Philip Wrage
Philip Wrage

Reputation: 1569

You need to set your Accept on the request method:

Response response= webTarget.request(MediaType.APPLICATION_JSON)
                       .header("Content-Type", "application/x-www-form-urlencoded")
                       .post(Entity.json(post));

You also need to ensure that if your API accepts application/x-www-form-urlencoded content, that is what you are sending.

Currently, you are sending application/json content based on your usage of Entity.json(post).

I don't know what type is assigned to post, but you need to figure out how to convert it either to a Form or a MultiValuedMap<String,String>, and then use the form method on Entity to submit your content.

Response response= webTarget.request(MediaType.APPLICATION_JSON)
                       .header("Content-Type", "application/x-www-form-urlencoded")
                       .post(Entity.form(postForm)); //assuming postForm typed as Form or MultiValuedMap<String,String>

Taking a guess regarding post, creating postForm as a MultiValuedMap<String,String> may be as simple as the following (which you would place prior to your request, of course).

MultiValuedMap<String,String> postForm = new MultiValuedHashMap<>();
postForm.add("client_credentials",scope);

Upvotes: 2

Related Questions