1

I am implementing a ReST api using Jersey client. The ReST works fine in POSTMAN. It takes 2 form-data parameters.

Following is my code.

package demo;

import java.net.URI;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;

import org.glassfish.jersey.client.ClientConfig;

public class ReSTCallToICRT {

    private static final String webServiceURI = "my-post-uri";

    public static void main(String[] args) {

        ClientConfig clientConfig = new ClientConfig();
        Client client = ClientBuilder.newClient(clientConfig);
        URI serviceURI = UriBuilder.fromUri(webServiceURI).build();
        WebTarget webTarget = client.target(serviceURI);

        Form form = new Form();

        form.param("parameter1", "value1");
        form.param("parameter2", "value2");

        String callResult = webTarget.request(MediaType.TEXT_PLAIN).post(Entity.entity(form,MediaType.MULTIPART_FORM_DATA_TYPE),String.class);

        System.out.println(callResult);

    }

}

While running this in eclipse as a Java project, I am getting the below error

Exception in thread "main" javax.ws.rs.InternalServerErrorException: HTTP 500 Internal Server Error
    at org.glassfish.jersey.client.JerseyInvocation.convertToException(JerseyInvocation.java:1020)
    at org.glassfish.jersey.client.JerseyInvocation.translate(JerseyInvocation.java:816)
    at org.glassfish.jersey.client.JerseyInvocation.access$700(JerseyInvocation.java:92)
    at org.glassfish.jersey.client.JerseyInvocation$2.call(JerseyInvocation.java:700)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:444)
    at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:696)
    at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:448)
    at org.glassfish.jersey.client.JerseyInvocation$Builder.post(JerseyInvocation.java:349)
    at demo.ReSTCallToICRT.main(ReSTCallToICRT.java:31)

Am I missing something here?

live2learn
  • 861
  • 2
  • 9
  • 16
  • 2
    It should be `MediaType.APPLICATION_FORM_URLENCODED`, assuming `application/x-www-form-urlencoded` media type is what you are trying to send. That's what `Form` is for. Or you can use the short form `Entity.form(form)`. If multipart is actually what you are trying to send, then you need to use a different API to build your entity. See [here](https://jersey.java.net/documentation/latest/media.html#d0e9278) if that's the case. – Paul Samsotha Mar 22 '16 at 16:20
  • @peeskillet With the above change, I am getting 'HTTP 400 Bad Request error' – live2learn Mar 22 '16 at 16:26
  • 1
    First answer this question. Are you trying to send `application/x-www-form-urlencdoed` data or `multipart/form-data` data? If the latter, you need to see my link. If the former, I can't really help. A 400 means a problem with the client request, but the request looks fine. – Paul Samsotha Mar 22 '16 at 16:29
  • If you're using Postman, I think the "form-data" button is to send multipart. So you might need to see the link. This would explain the 400. form-urlencoded data is in a completely different format than multipart – Paul Samsotha Mar 22 '16 at 16:32
  • 1
    Here's [another example](http://stackoverflow.com/a/27614403/2587435) – Paul Samsotha Mar 22 '16 at 16:43
  • @peeskillet It worked. Thanks a lot for your help!! – live2learn Mar 22 '16 at 16:48

0 Answers0