4

There is some documentation about sending post request in dispatch http://dispatch.databinder.net/Combined+Pages.html but yet it's not clear. What are myRequest and myPost there?

I want to send a https post request + add some cookies manually via headers + add some customs headers like form data, etc, and then read the response by reading the headers and cookies.

I only know how to prepare url for sending post request:

val url = host(myUrl + "?check=1&val1=123").secure

What do I do next?

Urist McDev
  • 498
  • 3
  • 14
Incerteza
  • 32,326
  • 47
  • 154
  • 261

1 Answers1

9

Dispatch is built on top of Async Http Client. Therefore, myRequest in the example:

 val myRequest = url("http://example.com/some/path")

is com.ning.http.client.RequestBuilder.

Calling the POST method on RequestBuilder turns the request into a POST request. That's what's happening in the myPost example:

def myPost = myRequest.POST

I often find the Dispatch documentation difficult to follow. For a quick overview of the all the various Dispatch operators, see: Periodic Table of Dispatch Operators

If you're asking how to build a POST request and add custom form parameters, you probably want to use the <<(values) operator like this:

val params = Map("param1" -> "val1", "param2" -> "val2") 
val req = url("http://www.example.com/some/path" <<(params)

Likewise if you want to add some custom headers you can use the <:<(map) operator like this:

val headers = Map("x-custom1" -> "val1", "x-custom2" -> "val2") 
val req = url("http://www.example.com/some/path" <<(params) <:<(headers)

Update: Actually, there is no POST method on RequestBuilder. The call to POST is part of Dispatch and calls setMethod on the underlying RequestBuilder. See dispatch.MethodVerbs for more details.

user24601
  • 1,662
  • 1
  • 12
  • 11
  • I think it's vise versa: <:< adds headers, << form parameters. – Incerteza Oct 09 '13 at 07:25
  • @Alex, I'm pretty sure I got it right. But your comment is one reason why I dislike all those crazy Dispatch operators. :-) In some cases I think it's clearer to call methods on `RequestBuilder` directly, such as: `req.addHeader("x-custom1", "val1")`. – user24601 Oct 09 '13 at 13:50