multipart/form-data 使用 scala-dispatch

multipart/form-data using scala-dispatch

我正在使用 scala-dispatch 库作为 http 客户端。我想要 post 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.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

我终于找到了问题的答案。

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