Http Status Code - Easiest Way to Get?

243 views
Skip to first unread message

Mike S

unread,
Oct 8, 2015, 8:27:33 PM10/8/15
to Dispatch
Hi all,

Dispatch newbie here.  Like the library so far, but for the life of me, I can't figure out how to get the status code if it's not an error status. 

I'm writing some tests for an HTTP service, and need to be able to distinguish between a 200/201/202, etc.  If anyone has any pointers or even better, a compile-able example, I'd sure appreciate it. 

Thanks,
-Mike

Miles Spielberg

unread,
Oct 8, 2015, 8:58:50 PM10/8/15
to dispatc...@googlegroups.com
The argument to OK or > is just a function (Response => T):

scala> Http(url("http://www.google.com") OK { x => x.getStatusCode} )
res11: dispatch.Future[Int] = scala.concurrent.impl.Promise$DefaultPromise@aa17f2c

scala> for (r <- res11) println(r)
200

The simplest function you could use here would be the identity function, after which you can extract the response from the future and process it however you like:

scala> Http(url("http://www.google.com") > identity)
res14: dispatch.Future[com.ning.http.client.Response] = scala.concurrent.impl.Promise$DefaultPromise@1b87c128

scala> res14.foreach { r =>
     | println(r.getStatusCode)
     | println(r.getResponseBody.take(100))
     | }

scala>
200
<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="en"><head><meta content

Miles Spielberg
Staff Software Engineer


O. 650.485.1102
4440 El Camino Real
Los Altos, CA 94022


--
You received this message because you are subscribed to the Google Groups "Dispatch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dispatch-scal...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages