POST request with JSON in Dispatch and Scala

I'm having trouble sending a POST request using a JSON object using Dispatch and Scala.

The POST request works fine, as I tested it with curl:

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

and I get an HTTP 200 response as expected

But when I try to do the same with the Dispatch library ( http://dispatch.databinder.net/Dispatch.html ):

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

here is what i get:

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)

I am not sure why it lacks the tracker parameter that I am trying to pass.

Anyone's thoughts?

+3
source share
2 answers

I think you want to publish it as a pair of name values:

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

, ( , 0.11.0) ( ), , :

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

. " > |" ( 0.8.x ) "OK as.String". .

, lift-json ( ).

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)

, . , JSON, . .

+4

All Articles