Response headers & InputStream

classic Classic list List threaded Threaded
14 messages Options
Reply | Threaded
Open this post in threaded view
|

Response headers & InputStream

zcox
Hi - just getting warmed up with Dispatch.  I need to issue a HTTP GET request and process both the response headers and the response body as an InputStream.  I see the >:> and >> HandlerVerbs but not sure how to use those together so the same block can process both response headers & body InputStream.

Any pointers would be greatly appreciated so I can use Dispatch goodness and not low-level HttpClient! :)

Thanks,
Zach
Reply | Threaded
Open this post in threaded view
|

Re: Response headers & InputStream

n8han
Administrator
A frequent question (that I will get into the docs, soon!). One way to do this is with  >+ which combines two handlers, seen in the specs here:
http://sourced.implicit.ly/net.databinder/dispatch-http.test/0.8.1/HttpSpec.scala.html#28372

Nathan

On 05/18/2011 06:06 PM, zcox [via Databinder] wrote:
Hi - just getting warmed up with Dispatch.  I need to issue a HTTP GET request and process both the response headers and the response body as an InputStream.  I see the >:> and >> HandlerVerbs but not sure how to use those together so the same block can process both response headers & body InputStream.

Any pointers would be greatly appreciated so I can use Dispatch goodness and not low-level HttpClient! :)

Thanks,
Zach


If you reply to this email, your message will be added to the discussion below:
http://databinder.3617998.n2.nabble.com/Response-headers-InputStream-tp6379722p6379722.html
To start a new topic under Databinder, email [hidden email]
To unsubscribe from Databinder, click here.

Reply | Threaded
Open this post in threaded view
|

Re: Response headers & InputStream

zcox
Perfect - thanks!  Here's what I ended up with, seems to work well:

  def http = new Http
  def mimeAndStreamFor(u: String): (Option[String], InputStream) =
    http(url(u) >+ { r =>
      (r >:> { h => h("Content-Type").headOption },
        r >> { is => is })
    })


Thanks,
Zach


On Wed, May 18, 2011 at 5:32 PM, n8han [via Databinder]
<[hidden email]> wrote:

> A frequent question (that I will get into the docs, soon!). One way to do
> this is with  >+ which combines two handlers, seen in the specs here:
> http://sourced.implicit.ly/net.databinder/dispatch-http.test/0.8.1/HttpSpec.scala.html#28372
>
> Nathan
>
> On 05/18/2011 06:06 PM, zcox [via Databinder] wrote:
>
> Hi - just getting warmed up with Dispatch.  I need to issue a HTTP GET
> request and process both the response headers and the response body as an
> InputStream.  I see the >:> and >> HandlerVerbs but not sure how to use
> those together so the same block can process both response headers & body
> InputStream.
>
> Any pointers would be greatly appreciated so I can use Dispatch goodness and
> not low-level HttpClient! :)
>
> Thanks,
> Zach
>
> ________________________________
> If you reply to this email, your message will be added to the discussion
> below:
> http://databinder.3617998.n2.nabble.com/Response-headers-InputStream-tp6379722p6379722.html
> To start a new topic under Databinder, email [hidden email]
> To unsubscribe from Databinder, click here.
>
>
> ________________________________
> If you reply to this email, your message will be added to the discussion
> below:
> http://databinder.3617998.n2.nabble.com/Response-headers-InputStream-tp6379722p6379808.html
> To unsubscribe from Response headers & InputStream, click here.
Reply | Threaded
Open this post in threaded view
|

Re: Response headers & InputStream

zcox
In reply to this post by n8han
Scratch that - looks like maybe the InputStream is closed outside the >> block?

I like to live on the edge, so this works:

  def http = new Http
  def mimeAndBytesFor(u: String): (Option[String], Array[Byte]) =
    http(url(u) >+ { r =>
      (r >:> { h => h("Content-Type").headOption },
        r >> { IOUtils.toByteArray(_) })
    })




On Thu, May 19, 2011 at 11:14 AM, Zach Cox <[hidden email]> wrote:

> Perfect - thanks!  Here's what I ended up with, seems to work well:
>
>  def http = new Http
>  def mimeAndStreamFor(u: String): (Option[String], InputStream) =
>    http(url(u) >+ { r =>
>      (r >:> { h => h("Content-Type").headOption },
>        r >> { is => is })
>    })
>
>
> Thanks,
> Zach
>
>
> On Wed, May 18, 2011 at 5:32 PM, n8han [via Databinder]
> <[hidden email]> wrote:
>> A frequent question (that I will get into the docs, soon!). One way to do
>> this is with  >+ which combines two handlers, seen in the specs here:
>> http://sourced.implicit.ly/net.databinder/dispatch-http.test/0.8.1/HttpSpec.scala.html#28372
>>
>> Nathan
>>
>> On 05/18/2011 06:06 PM, zcox [via Databinder] wrote:
>>
>> Hi - just getting warmed up with Dispatch.  I need to issue a HTTP GET
>> request and process both the response headers and the response body as an
>> InputStream.  I see the >:> and >> HandlerVerbs but not sure how to use
>> those together so the same block can process both response headers & body
>> InputStream.
>>
>> Any pointers would be greatly appreciated so I can use Dispatch goodness and
>> not low-level HttpClient! :)
>>
>> Thanks,
>> Zach
>>
>> ________________________________
>> If you reply to this email, your message will be added to the discussion
>> below:
>> http://databinder.3617998.n2.nabble.com/Response-headers-InputStream-tp6379722p6379722.html
>> To start a new topic under Databinder, email [hidden email]
>> To unsubscribe from Databinder, click here.
>>
>>
>> ________________________________
>> If you reply to this email, your message will be added to the discussion
>> below:
>> http://databinder.3617998.n2.nabble.com/Response-headers-InputStream-tp6379722p6379808.html
>> To unsubscribe from Response headers & InputStream, click here.
>
Reply | Threaded
Open this post in threaded view
|

Re: Response headers & InputStream

n8han
Administrator
On 05/19/2011 01:04 PM, zcox [via Databinder] wrote:
> Scratch that - looks like maybe the InputStream is closed outside the
> >> block?

That's right. If you want to have the headers and body in the same scope
(so you can decide whether/how to read the stream using the headers)
there's a chaining method >+>

http://sourced.implicit.ly/net.databinder/dispatch-http.test/0.8.1/HttpSpec.scala.html#28761

Nathan
Reply | Threaded
Open this post in threaded view
|

Re: Response headers & InputStream

regadas
Hello I'm very new to dispatch but I'm facing almost the same problem. I need to parse a json response and check the header. Currently I'm just processing the response using the '>#'

Can you point me to any resource where I can achieve this?

Thank you.
Reply | Threaded
Open this post in threaded view
|

Re: Response headers & InputStream

n8han
Administrator
Hi, there's a new documentation page on this topic:
http://dispatch.databinder.net/Two+Handlers+Are+Better+Than+One.html

Please have a read through it and write back if you are still stuck.

Nathan
Reply | Threaded
Open this post in threaded view
|

Re: Response headers & InputStream

regadas
Hi Nathan, thanks for quick reply :)

Well I'm trying to do the following:

http(show(name) >+ { req =>
          (
           req >:> { _("Last-Modified") },
           req ># { js => for { JString(someting) <- js \ "someting" } yield something }
          )
        })

However I'm getting that ># does not belong to HandlerVerbs. I got the felling that I'm missing something :/.

Thanks for your help.
Reply | Threaded
Open this post in threaded view
|

Re: Response headers & InputStream

n8han
Administrator
Did you
import dispatch.liftjson.Js._

? That is what would normally pull ># into scope, but maybe there is some reason
the implicit conversion is not taking place there.

Nathan

On 07/20/2011 06:09 PM, regadas [via Databinder] wrote:
Hi Nathan, thanks for quick reply :)

Well I'm trying to do the following:

http(show(name) >+ { req =>
          (
           req >:> { _("Last-Modified") },
           req ># { js => for { JString(someting) <- js \ "someting" } yield something }
          )
        })

However I'm getting that ># does not belong to HandlerVerbs. I got the felling that I'm missing something :/.

Thanks for your help.


If you reply to this email, your message will be added to the discussion below:
http://databinder.3617998.n2.nabble.com/Response-headers-InputStream-tp6379722p6604595.html
To start a new topic under Databinder, email [hidden email]
To unsubscribe from Databinder, click here.

Reply | Threaded
Open this post in threaded view
|

Re: Response headers & InputStream

regadas
Hi Nathan!

yup I'm using the following imports:

import dispatch._
import dispatch.liftjson.Js._
import net.liftweb.json.JsonAST._

Reply | Threaded
Open this post in threaded view
|

Re: Response headers & InputStream

n8han
Administrator
Alright, looks like the conversions in Dispatch's lift-json module declare types that are too specific. I opened an issue and will try to get a release out this weekend.
https://github.com/n8han/Databinder-Dispatch/issues/40

If you want to code a workaround in the meantime, there is not *too much* code to write.
https://github.com/n8han/Databinder-Dispatch/blob/master/lift-json/src/main/scala/LiftJson.scala

Nathan
Reply | Threaded
Open this post in threaded view
|

Re: Response headers & InputStream

n8han
Administrator
This is fixed in 0.8.4: http://post.ly/2cbJq

Nathan
Reply | Threaded
Open this post in threaded view
|

Re: Response headers & InputStream

jeantil
In reply to this post by n8han
Hi,

I am fairly new to scala and the functionnal world in general and I find it pretty hard to wrap my head around dispatch examples. In your examples the response processing is fairly basic and can fit in 5-10 lines.

Consider the case where there is a very complex analysis of the headers then multiple extractions from the response. the code for these would be dozens of lines long.

How do I extract this logic out of the current scope for reuse or readability ?
Reply | Threaded
Open this post in threaded view
|

Re: Response headers & InputStream

n8han
Administrator
Could you repost this question on the new list?

https://groups.google.com/forum/?hl=en#!forum/dispatch-scala

On 07/28/2011 10:54 AM, jeantil [via Databinder] wrote:
Hi,

I am fairly new to scala and the functionnal world in general and I find it pretty hard to wrap my head around dispatch examples. In your examples the response processing is fairly basic and can fit in 5-10 lines.

Consider the case where there is a very complex analysis of the headers then multiple extractions from the response. the code for these would be dozens of lines long.

How do I extract this logic out of the current scope for reuse or readability ?


If you reply to this email, your message will be added to the discussion below:
http://databinder.3617998.n2.nabble.com/Response-headers-InputStream-tp6379722p6630133.html
To start a new topic under Databinder, email [hidden email]
To unsubscribe from Databinder, click here.