Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending a custom POST https request in dispatch (+cookies, + headers)

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?

like image 664
Incerteza Avatar asked Jan 12 '23 04:01

Incerteza


1 Answers

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.

like image 70
user24601 Avatar answered Jan 30 '23 10:01

user24601