在Dispatch和Scala中使用JSON的POST请求

时间:2012-05-22 22:26:24

标签: json scala post scala-dispatch

使用Dispatch和Scala在使用JSON对象发出POST请求时遇到问题。

自从我使用curl测试它后,POST请求工作正常:

~/workspace $ curl -vd 'tracker={"project":"campaign","event":"home","number":"100"}'  http://exampleurl:8145/test

我按预期获得了HTTP 200响应

但是当我尝试使用Dispatch库(http://dispatch.databinder.net/Dispatch.html)这样做时:

scala> Http(url("http://exampleurl:8145/test") << "tracker={'project':'campaign','event':'home','number':'100'}" >|)

这就是我得到的:

dispatch.StatusCode: Unexpected response code: 404
Paramerter missing: [tracker] not found
at dispatch.HttpExecutor$$anonfun$when$1.apply(executor.scala:53)
at dispatch.HttpExecutor$$anonfun$when$1.apply(executor.scala:50)
at dispatch.HttpExecutor$$anonfun$x$2.apply(executor.scala:41)
at dispatch.HttpExecutor$$anonfun$x$2.apply(executor.scala:36)
at dispatch.BlockingHttp$$anonfun$execute$1.apply(Http.scala:54)
at dispatch.Http.pack(Http.scala:25)
at dispatch.BlockingHttp$class.execute(Http.scala:53)
at dispatch.Http.execute(Http.scala:21)
at dispatch.HttpExecutor$class.x(executor.scala:36)
at dispatch.Http.x(Http.scala:21)
at dispatch.HttpExecutor$class.when(executor.scala:50)
at dispatch.Http.when(Http.scala:21)
at dispatch.HttpExecutor$class.apply(executor.scala:60)
at dispatch.Http.apply(Http.scala:21)
at .<init>(<console>:11)
at .<clinit>(<console>)
at .<init>(<console>:11)
at .<clinit>(<console>)
at $print(<console>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:704)
at scala.tools.nsc.interpreter.IMain$Request.loadAndRun(IMain.scala:914)
at scala.tools.nsc.interpreter.IMain.loadAndRunReq$1(IMain.scala:546)
at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:577)
at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:543)
at scala.tools.nsc.interpreter.ILoop.reallyInterpret$1(ILoop.scala:694)
at scala.tools.nsc.interpreter.ILoop.interpretStartingWith(ILoop.scala:745)
at scala.tools.nsc.interpreter.ILoop.command(ILoop.scala:651)
at scala.tools.nsc.interpreter.ILoop.processLine$1(ILoop.scala:542)
at scala.tools.nsc.interpreter.ILoop.loop(ILoop.scala:550)
at scala.tools.nsc.interpreter.ILoop.process(ILoop.scala:822)
at scala.tools.nsc.interpreter.ILoop.main(ILoop.scala:851)
at xsbt.ConsoleInterface.run(ConsoleInterface.scala:57)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at sbt.compiler.AnalyzingCompiler.call(AnalyzingCompiler.scala:57)
at sbt.compiler.AnalyzingCompiler.console(AnalyzingCompiler.scala:48)
at sbt.Console.console0$1(Console.scala:23)
at sbt.Console$$anonfun$apply$2$$anonfun$apply$1.apply$mcV$sp(Console.scala:24)
at sbt.TrapExit$.executeMain$1(TrapExit.scala:33)
at sbt.TrapExit$$anon$1.run(TrapExit.scala:42)

我不确定为什么它错过了我试图通过的跟踪器参数。

有人想到吗?

2 个答案:

答案 0 :(得分:4)

我认为您希望将其作为名称值对发布:

Http(url("http://exampleurl:8145/test") << 
  Map("tracker" -> "{'project':'campaign','event':'home','number':'100'}") >|)

答案 1 :(得分:4)

不确定您的调度版本,但另一种方法(至少在版本0.11.0中)(特别是如果不方便将数据混搭回Map)是明确设置内容类型,像这样:

Http(url("http://exampleurl:8145/test").setBody(
     "tracker={'project':'campaign','event':'home','number':'100'}").setHeader(
     "Content-Type", "application/json") OK as.String)

注意:我删除了“&gt; |”使用“OK as.String”运算符(来自dispatch 0.8.x并忽略响应)。随意忽略该响应字符串。

如果你正在使用lift-json来处理案例类(或者只是使用AST进行修改),那么这很好。然后而不是

Http(url("http://someplace.com") << someAwfulTransformer(
     myCaseClassOrBigASTbasedThing) OK as.String)

你可以这样做

Http(url("http://someplace.com").setBody(
     write(myCaseClassOrBigASTbasedThing)).setHeader(
     "Content-Type", "application/json") OK as.String)

随意用你的implicits或帮助函数来装饰它,以尽量减少打字。但重点是,如果您有一个直接实体到JSON字符串映射,您不必担心将其转换回Map。实际上,你不应该