1

I am trying to post form data through a JAVA Jersey REST client but i receive the response code 500 and an according exception:

java.lang.RuntimeException: Failed with HTTP error code : 500

The same request from POSTMAN(Chrome Extention) works successfully.

I am making a POST request to StreamSets Data Collector API.

Below is my Code

public static String testUploadService(String httpURL, File filePath)  throws Exception {

    // local variables
    ClientConfig clientConfig = null;
    Client client = null;
    WebTarget webTarget = null;
    Invocation.Builder invocationBuilder = null;
    Response response = null;
    FileDataBodyPart fileDataBodyPart = null;
    FormDataMultiPart formDataMultiPart = null;
    int responseCode;
    String responseMessageFromServer = null;
    String responseString = null;
      String name = "*******";
    String password = "*******";
    String authString = name + ":" + password;
    String sdc="sdc";
    byte[] encoding = Base64.getEncoder().encode(authString.getBytes());
    byte[] encoding2 = Base64.getEncoder().encode(sdc.getBytes());
    String USER_PASS = new String(encoding);
    String auth2=new String(encoding2);

    try{

        ClientConfig cc = new ClientConfig();
        cc.register(MultiPartFeature.class);

        try {
        client = new JerseywithSSL().initClient(cc);
        } catch (KeyManagementException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) { 
        e.printStackTrace();
        }
        webTarget = client.target(httpURL);

        // set file upload values
        fileDataBodyPart = new FileDataBodyPart("uploadFile", filePath, MediaType.MULTIPART_FORM_DATA_TYPE);
        formDataMultiPart = new FormDataMultiPart();
        formDataMultiPart.bodyPart(fileDataBodyPart);

        // invoke service
        invocationBuilder = webTarget.request();
                  invocationBuilder.header("Authorization", "Basic " + USER_PASS);
                  invocationBuilder.header("X-Requested-By","SDC"); //Additional Header requiered by Streamsets RestAPI
                 invocationBuilder.header("Content-type", "multipart/form-data");
        response = invocationBuilder.post(Entity.entity(formDataMultiPart, MediaType.MULTIPART_FORM_DATA));

        // get response code
        responseCode = response.getStatus();
        System.out.println("Response code: " + responseCode);

        if (response.getStatus() != 200) {
            throw new RuntimeException("Failed with HTTP error code : " + responseCode);
        }

        // get response message
        responseMessageFromServer = response.getStatusInfo().getReasonPhrase();
        System.out.println("ResponseMessageFromServer: " + responseMessageFromServer);

        // get response string
        responseString = response.readEntity(String.class);
    }
    catch(Exception ex) {
        ex.printStackTrace();
    }
    finally{
        // release resources, if any
        fileDataBodyPart.cleanup();
        formDataMultiPart.cleanup();
        formDataMultiPart.close();
        response.close();
        client.close();
    }
    return responseString;
}

}

And here is screenshot of POSTMAN of all header and Authentication included,

POSTMAN SCREENSHOT

POSTMAN SCREENSHOT

POSTMAN SCREENSHOT

POSTMAN SCREENSHOT

I can't figure out whether its an issue with forming a multipart or is it an issue on the server side and if its the former than where exactly am I going wrong?

PS: I got over SSL certificate error by adding Trust certificate.

UPDATE 1

After I dig further into I got Following error stacktrace.

responseString : {
  "RemoteException" : {
"message" : "java.lang.NullPointerException: in is null",
"errorCode" : "CONTAINER_0000",
"localizedMessage" : "in is null",
"exception" : "NullPointerException",
"javaClassName" : "java.lang.NullPointerException",
"stackTrace" : "java.lang.NullPointerException: in is null\n\tat java.util.zip.ZipInputStream.<init>(ZipInputStream.java:101)\n\tat java.util.zip.ZipInputStream.<init>(ZipInputStream.java:80)\n\tat com.streamsets.datacollector.restapi.PipelineStoreResource.importPipelines(PipelineStoreResource.java:551)\n\tat sun.reflect.GeneratedMethodAccessor573.invoke(Unknown Source)\n\tat sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\n\tat java.lang.reflect.Method.invoke(Method.java:498)\n\tat org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)\n\tat org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:144)\n\tat org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:161)\n\tat org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$ResponseOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:160)\n\tat org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:99)\n\tat org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389)\n\tat org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347)\n\tat org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)\n\tat org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:326)\n\tat org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)\n\tat org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)\n\tat org.glassfish.jersey.internal.Errors.process(Errors.java:315)\n\tat org.glassfish.jersey.internal.Errors.process(Errors.java:297)\n\tat org.glassfish.jersey.internal.Errors.process(Errors.java:267)\n\tat org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)\n\tat org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:305)\n\tat org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1154)\n\tat org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:473)\n\tat org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:427)\n\tat org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:388)\n\tat org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:341)\n\tat org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:228)\n\tat org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:841)\n\tat org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1634)\n\tat com.streamsets.datacollector.http.GroupsInScopeFilter.lambda$doFilter$0(GroupsInScopeFilter.java:82)\n\tat com.streamsets.datacollector.security.GroupsInScope.execute(GroupsInScope.java:33)\n\tat com.streamsets.datacollector.http.GroupsInScopeFilter.doFilter(GroupsInScopeFilter.java:81)\n\tat org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1621)\n\tat org.eclipse.jetty.servlets.CrossOriginFilter.handle(CrossOriginFilter.java:308)\n\tat org.eclipse.jetty.servlets.CrossOriginFilter.doFilter(CrossOriginFilter.java:262)\n\tat org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1621)\n\tat com.streamsets.datacollector.http.LocaleDetectorFilter.doFilter(LocaleDetectorFilter.java:39)\n\tat org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1621)\n\tat com.streamsets.pipeline.http.MDCFilter.doFilter(MDCFilter.java:47)\n\tat org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1621)\n\tat org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:541)\n\tat org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)\n\tat org.eclipse.jetty.server.handler.gzip.GzipHandler.handle(GzipHandler.java:494)\n\tat org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:513)\n\tat org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)\n\tat org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:190)\n\tat org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1592)\n\tat org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:188)\n\tat org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1239)\n\tat org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:168)\n\tat org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:481)\n\tat org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1561)\n\tat org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:166)\n\tat org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1141)\n\tat org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)\n\tat org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)\n\tat org.eclipse.jetty.rewrite.handler.RewriteHandler.handle(RewriteHandler.java:335)\n\tat org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:118)\n\tat org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:213)\n\tat org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)\n\tat org.eclipse.jetty.server.Server.handle(Server.java:564)\n\tat org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:320)\n\tat org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:251)\n\tat org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:279)\n\tat org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:110)\n\tat org.eclipse.jetty.io.ssl.SslConnection.onFillable(SslConnection.java:258)\n\tat org.eclipse.jetty.io.ssl.SslConnection$3.succeeded(SslConnection.java:147)\n\tat org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:110)\n\tat org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:124)\n\tat org.eclipse.jetty.util.thread.Invocable.invokePreferred(Invocable.java:122)\n\tat org.eclipse.jetty.util.thread.strategy.ExecutingExecutionStrategy.invoke(ExecutingExecutionStrategy.java:58)\n\tat org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.produceConsume(ExecuteProduceConsume.java:201)\n\tat org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.run(ExecuteProduceConsume.java:133)\n\tat org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:672)\n\tat org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:590)\n\tat java.lang.Thread.run(Thread.java:748)\n"
  }
}
Vijay Shekhawat
  • 149
  • 2
  • 13
  • 2
    Just a quick one - It doesn't really matter if you hide the Auth `username` in the Postman image because the `Basic` auth is just base64 encoded so it can be reversed from your other Postman image showing the headers. – Danny Dainton Jan 17 '18 at 12:02
  • @DannyDainton thanks for pointing that out – Vijay Shekhawat Jan 17 '18 at 12:17
  • If you have code 500, my first reflex would be that there is something wrong in the server. If your client messed up and sent a bad request, the server is supposed to return 400 with the exception. The fact that it returns 500 means that there is some uncaught exception while parsing the request. Try running your server under a debugger and debug the request parsing cycle. – suenda Jan 17 '18 at 12:32
  • @Suenda I understand that code 500 means, there is an error on the server side but what is confusing is why postman does not give the similar error? and this StreamSets Data Collector API I am calling, I don't have access to the server. – Vijay Shekhawat Jan 17 '18 at 12:58
  • @suenda I have included the stack trace as an update, does that give any further insight? – Vijay Shekhawat Jan 18 '18 at 08:24
  • @VijayShekhawat well, there is a NullPointerException on the server side. You need to see what makes your JAX-RS client request different from the one made by Postman. My first step towards debugging would be to pass the HTTP request through a proxy, intercept the request and see what is exactly being exchanged between client and server and find the difference between two requests. Have a look at a tool called Fiddler (https://www.telerik.com/fiddler). I am sorry I am not in any way familiar with the API service you are calling. Maybe post a bug in your API service ? – suenda Jan 18 '18 at 18:33
  • From the error logs, it seems that the zip file that you are trying to upload from client program is not valid or empty. Check whether you are giving correct path of the valid file. – Vikas Sachdeva Jan 19 '18 at 02:53

0 Answers0