2016-04-06 86 views
2

我使用scala-dispatch庫作爲http客戶端。我想用表單值發佈multipart/form-data。誰能幫我?使用scala-dispatch的multipart/form-data

我的代碼:

val host_url : Req = url("http://localhost:8080/multipart-sample/profile/uploadfile").POST.setContentType("multipart/form-data", "UTF-8") 
val request = host_url.addBodyPart(new FilePart("imagefile", new File("/path/to/file"))).addParameter("name", "sample_name").addParameter("age", "sample_age").addParameter("gender", "gender") 
Http(request OK as.String) onComplete { 
case Success(s) => println(s) 
case Failure(e) => e.printStackTrace 
} 

我碰到下面的錯誤。我的服務器不會引發任何錯誤。所以500不是來自服務器。

java.util.concurrent.ExecutionException: dispatch.StatusCode: Unexpected response status: 500 
at com.ning.http.client.providers.netty.future.NettyResponseFuture.abort(NettyResponseFuture.java:229) 
at com.ning.http.client.providers.netty.request.NettyRequestSender.abort(NettyRequestSender.java:416) 
at com.ning.http.client.providers.netty.handler.HttpProtocol.handle(HttpProtocol.java:492) 
at com.ning.http.client.providers.netty.handler.Processor.messageReceived(Processor.java:89) 
at org.jboss.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:70) 
at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564) 
at org.jboss.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext.sendUpstream(DefaultChannelPipeline.java:791) 
at org.jboss.netty.handler.stream.ChunkedWriteHandler.handleUpstream(ChunkedWriteHandler.java:142) 
at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564) 
at org.jboss.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext.sendUpstream(DefaultChannelPipeline.java:791) 
at org.jboss.netty.handler.codec.http.HttpContentDecoder.messageReceived(HttpContentDecoder.java:108) 
at org.jboss.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:70) 
at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564) 
at org.jboss.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext.sendUpstream(DefaultChannelPipeline.java:791) 
at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:296) 
at org.jboss.netty.handler.codec.frame.FrameDecoder.unfoldAndFireMessageReceived(FrameDecoder.java:459) 
at org.jboss.netty.handler.codec.replay.ReplayingDecoder.callDecode(ReplayingDecoder.java:536) 
at org.jboss.netty.handler.codec.replay.ReplayingDecoder.messageReceived(ReplayingDecoder.java:485) 
at org.jboss.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:70) 
at org.jboss.netty.handler.codec.http.HttpClientCodec.handleUpstream(HttpClientCodec.java:92) 
at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564) 
at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:559) 
at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:268) 
at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:255) 
at org.jboss.netty.channel.socket.nio.NioWorker.read(NioWorker.java:88) 
at org.jboss.netty.channel.socket.nio.AbstractNioWorker.process(AbstractNioWorker.java:108) 
at org.jboss.netty.channel.socket.nio.AbstractNioSelector.run(AbstractNioSelector.java:337) 
at org.jboss.netty.channel.socket.nio.AbstractNioWorker.run(AbstractNioWorker.java:89) 
at org.jboss.netty.channel.socket.nio.NioWorker.run(NioWorker.java:178) 
at org.jboss.netty.util.ThreadRenamingRunnable.run(ThreadRenamingRunnable.java:108) 
at org.jboss.netty.util.internal.DeadLockProofWorker$1.run(DeadLockProofWorker.java:42) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 
at java.lang.Thread.run(Thread.java:745) 
Caused by: dispatch.StatusCode: Unexpected response status: 500 
at dispatch.OkHandler$class.onStatusReceived(handlers.scala:37) 
at dispatch.OkFunctionHandler.onStatusReceived(handlers.scala:29) 
at com.ning.http.client.providers.netty.handler.HttpProtocol.exitAfterHandlingStatus(HttpProtocol.java:378) 
at com.ning.http.client.providers.netty.handler.HttpProtocol.handleHttpResponse(HttpProtocol.java:429) 
at com.ning.http.client.providers.netty.handler.HttpProtocol.handle(HttpProtocol.java:476) 
... 31 more 

回答

1

最後我找到了我的問題的答案。

val request = host_url.addBodyPart(new FilePart("imagefile", new File("/path/to/file"))).addParameter("name", "sample_name").addParameter("age", "sample_age").addParameter("gender", "gender")

具有以下

val request = host_url.addBodyPart(new FilePart("imagefile", new File("/path/to/file"))).addBodyPart(new StringPart("param", "value"))

替換此
相關問題