简体   繁体   中英

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.

在此处输入图像描述

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);

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.

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 aForm 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);

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM