I had developed HTML form multipart data request wrapper at server end which reads form-data and calls post API with those data. But Unirest POST API call fails when passing file data with wrong credentials. Using postman
tool for the same case, it's giving proper response 401 - User Unauthorized
.
Wrapper code:
@POST
@Timed
@UnitOfWork
@Path("/add")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_HTML)
public View addFirmwareView(@Context UriInfo uriInfo, @FormDataParam("field1") String field1,
@FormDataParam("field2") String field2, @FormDataParam("field3") String field3,
@FormDataParam("file") final InputStream stream,
@FormDataParam("file") final FormDataContentDisposition contentDisposition,
@FormDataParam("username") String username, @FormDataParam("password") String password) {
JSONObject json = new JSONObject();
json.put("field1", field1);
json.put("field2", field2);
json.put("field3", field3);
String jsonFirmware = json.toString();
String url = BASE_URL + SEPARATOR + FIRMWARE_URL;
L.info("Add url: {}", url);
HttpRequest httpRequest = Unirest.post(url)
.header("Authorization", "Basic " + Base64Coder.encodeString(username + ":" + password))
.header("cache-control", "no-cache")
.field("file", stream, ContentType.APPLICATION_OCTET_STREAM, contentDisposition.getFileName())
.field("firmware", jsonFirmware).getHttpRequest();
HttpResponse<String> httpResponse = httpRequest.asString();
return httpResponse;
}
Error logs:
web_1 | INFO [2017-08-31 09:44:15,462] com.firmwareapi.security.BasicAuthenticator: User and Password does not match.
web_1 | 172.17.0.1 - - [31/Aug/2017:09:44:15 +0000] "POST /v1/firmware HTTP/1.1" 401 78 "-" "unirest-java/1.3.11" 4
web_1 | INFO [2017-08-31 09:44:15,468] org.apache.http.impl.execchain.RetryExec: I/O exception (java.net.SocketException) caught when processing request to {}->http://192.168.99.100:8080: Broken pipe
web_1 | INFO [2017-08-31 09:44:15,468] com.firmwareapi.resources.FirmwareViewResource: AddFirmwareView error: org.apache.http.client.ClientProtocolException, class: com.mashape.unirest.http.exceptions.UnirestException
web_1 | com.mashape.unirest.http.exceptions.UnirestException: org.apache.http.client.ClientProtocolException
web_1 | at com.mashape.unirest.http.HttpClientHelper.request(HttpClientHelper.java:143)
web_1 | at com.mashape.unirest.request.BaseRequest.asString(BaseRequest.java:56)
web_1 | at com.firmwareapi.helper.APIHelper.addFirmware(APIHelper.java:36)
web_1 | at com.firmwareapi.resources.FirmwareViewResource.addFirmwareView(FirmwareViewResource.java:137)
web_1 | at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
web_1 | at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
web_1 | at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
web_1 | at java.lang.reflect.Method.invoke(Method.java:498)
web_1 | at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)
web_1 | at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:144)
web_1 | at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:161)
web_1 | at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:205)
web_1 | at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:99)
web_1 | at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389)
web_1 | at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347)
web_1 | at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)
web_1 | at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:326)
web_1 | at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
web_1 | at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
web_1 | at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
web_1 | at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
web_1 | at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
web_1 | at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
web_1 | at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:305)
web_1 | at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1154)
web_1 | at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:473)
web_1 | at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:427)
web_1 | at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:388)
web_1 | at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:341)
web_1 | at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:228)
web_1 | at io.dropwizard.jetty.NonblockingServletHolder.handle(NonblockingServletHolder.java:49)
web_1 | at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1634)
web_1 | at io.dropwizard.servlets.ThreadNameFilter.doFilter(ThreadNameFilter.java:34)
web_1 | at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1621)
web_1 | at io.dropwizard.jersey.filter.AllowedMethodsFilter.handle(AllowedMethodsFilter.java:50)
web_1 | at io.dropwizard.jersey.filter.AllowedMethodsFilter.doFilter(AllowedMethodsFilter.java:44)
web_1 | at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1621)
web_1 | at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:541)
web_1 | at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:188)
web_1 | at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1239)
web_1 | at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:168)
web_1 | at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:481)
web_1 | at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:166)
web_1 | at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1141)
web_1 | at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
web_1 | at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
web_1 | at com.codahale.metrics.jetty9.InstrumentedHandler.handle(InstrumentedHandler.java:241)
web_1 | at io.dropwizard.jetty.ContextRoutingHandler.handle(ContextRoutingHandler.java:38)
web_1 | at org.eclipse.jetty.server.handler.gzip.GzipHandler.handle(GzipHandler.java:494)
web_1 | at io.dropwizard.jetty.BiDiGzipHandler.handle(BiDiGzipHandler.java:68)
web_1 | at org.eclipse.jetty.server.handler.RequestLogHandler.handle(RequestLogHandler.java:56)
web_1 | at org.eclipse.jetty.server.handler.StatisticsHandler.handle(StatisticsHandler.java:169)
web_1 | at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
web_1 | at org.eclipse.jetty.server.Server.handle(Server.java:564)
web_1 | at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:320)
web_1 | at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:251)
web_1 | at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:279)
web_1 | at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:110)
web_1 | at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:124)
web_1 | at org.eclipse.jetty.util.thread.Invocable.invokePreferred(Invocable.java:122)
web_1 | at org.eclipse.jetty.util.thread.strategy.ExecutingExecutionStrategy.invoke(ExecutingExecutionStrategy.java:58)
web_1 | at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.produceConsume(ExecuteProduceConsume.java:201)
web_1 | at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.run(ExecuteProduceConsume.java:133)
web_1 | at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:672)
web_1 | at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:590)
web_1 | at java.lang.Thread.run(Thread.java:745)
web_1 | Caused by: org.apache.http.client.ClientProtocolException
web_1 | at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:186)
web_1 | at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
web_1 | at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:107)
web_1 | at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55)
web_1 | at com.mashape.unirest.http.HttpClientHelper.request(HttpClientHelper.java:138)
web_1 | ... 65 more
web_1 | Caused by: org.apache.http.client.NonRepeatableRequestException: Cannot retry request with a non-repeatable request entity
web_1 | at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:107)
web_1 | at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
web_1 | at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
web_1 | ... 69 more
web_1 | Caused by: java.net.SocketException: Broken pipe
web_1 | at java.net.SocketOutputStream.socketWrite0(Native Method)
web_1 | at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109)
web_1 | at java.net.SocketOutputStream.write(SocketOutputStream.java:153)
web_1 | at org.apache.http.impl.io.SessionOutputBufferImpl.streamWrite(SessionOutputBufferImpl.java:126)
web_1 | at org.apache.http.impl.io.SessionOutputBufferImpl.flushBuffer(SessionOutputBufferImpl.java:138)
web_1 | at org.apache.http.impl.io.SessionOutputBufferImpl.write(SessionOutputBufferImpl.java:169)
web_1 | at org.apache.http.impl.io.ChunkedOutputStream.flushCacheWithAppend(ChunkedOutputStream.java:124)
web_1 | at org.apache.http.impl.io.ChunkedOutputStream.write(ChunkedOutputStream.java:181)
web_1 | at org.apache.http.entity.mime.content.InputStreamBody.writeTo(InputStreamBody.java:93)
web_1 | at org.apache.http.entity.mime.AbstractMultipartForm.doWriteTo(AbstractMultipartForm.java:134)
web_1 | at org.apache.http.entity.mime.AbstractMultipartForm.writeTo(AbstractMultipartForm.java:157)
web_1 | at org.apache.http.entity.mime.MultipartFormEntity.writeTo(MultipartFormEntity.java:103)
web_1 | at org.apache.http.impl.execchain.RequestEntityProxy.writeTo(RequestEntityProxy.java:123)
web_1 | at org.apache.http.impl.DefaultBHttpClientConnection.sendRequestEntity(DefaultBHttpClientConnection.java:158)
web_1 | at org.apache.http.impl.conn.CPoolProxy.sendRequestEntity(CPoolProxy.java:162)
web_1 | at org.apache.http.protocol.HttpRequestExecutor.doSendRequest(HttpRequestExecutor.java:237)
web_1 | at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:122)
web_1 | at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:271)
web_1 | at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
web_1 | at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
web_1 | ... 71 more
web_1 | INFO [2017-08-31 09:44:15,503] org.hibernate.engine.internal.StatisticalLoggingSessionEventListener: Session Metrics {
web_1 | 85814 nanoseconds spent acquiring 2 JDBC connections;
web_1 | 134982 nanoseconds spent releasing 2 JDBC connections;
web_1 | 0 nanoseconds spent preparing 0 JDBC statements;
web_1 | 0 nanoseconds spent executing 0 JDBC statements;
web_1 | 0 nanoseconds spent executing 0 JDBC batches;
web_1 | 0 nanoseconds spent performing 0 L2C puts;
web_1 | 0 nanoseconds spent performing 0 L2C hits;
web_1 | 0 nanoseconds spent performing 0 L2C misses;
web_1 | 0 nanoseconds spent executing 0 flushes (flushing a total of 0 entities and 0 collections);
web_1 | 0 nanoseconds spent executing 0 partial-flushes (flushing a total of 0 entities and 0 collections)
web_1 | }
I spent a lot of time to figure out this weird issue but won't be able to fix it. If anyone have any possible solution. Please share.