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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With