Created
May 6, 2013 10:28
-
-
Save fracek/5524366 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module: client-test | |
synopsis: | |
author: Francesco Ceccon | |
define function request-with-params () | |
let p = table(<string-table>, "param1" => "value1", "param2" => "value2"); | |
/* | |
* Shortcut to add params to the request | |
*/ | |
let response = http-get("http://httpbin.org/get", params: p); | |
end function request-with-params; | |
define function custom-headers () | |
let h = table(<string-table>, "Content-Type" => "application/json"); | |
/* | |
* Add custom headers to a request | |
*/ | |
let response = http-get("http://httpbin.org/get", headers: h); | |
end function; | |
define function post-request-form () | |
/* | |
* Shortcut to add form encoded key-values to a POST request | |
* Post request also accepts custom headers | |
*/ | |
let payload = table(<string-table>, "key1" => "value1", "key2" => "value2"); | |
let response = http-post("http://httpbin.org/post", data: payload); | |
end function post-request-form; | |
define function post-request-string () | |
/* | |
* If we pass a string send it as-is | |
*/ | |
let payload = table(<string-table>, "key1" => "value1", "key2" => "value2"); | |
let response = http-post("http://httpbin.org/post", | |
data: write-object-to-json-string(payload)); | |
end function post-request-string; | |
/* | |
* Requests like | |
*/ | |
define function post-multipart-file () | |
/* | |
* Not sure if we really need to pass a table (like Requests), or it's | |
* enough to pass a <file-stream> directly. | |
*/ | |
let fs = make(<file-stream>, locator: "foo.txt"); | |
let f = table(<string-table>, "file" => fs); | |
let response = http-post("http://httpbin.org/post", files: f); | |
end function; | |
/* | |
* How I would do it. I need to know why Requests uses a table. | |
*/ | |
define function post-multipart-file () | |
let fs = make(<file-stream>, locator: "foo.txt"); | |
let f = table(<string-table>, "file" => fs, "name" => "bar.txt"); | |
let response = http-post("http://httpbin.org/post", files: f); | |
end function; | |
define function head-request () | |
let response = http-head("http://httpbin.org/post"); | |
end function; | |
define function delete-request () | |
let response = http-delete("http://httpbin.org/delete"); | |
end function; | |
define function http-authentication () | |
/* | |
* Extend a base class <http-authentication> to provide more authentication | |
* methods | |
*/ | |
let auth = make(<http-basic-authentication>, username: "foo@bar.com", | |
password: "aaaaaa"); | |
let response = http-post("http://httpbin.org/", auth: auth); | |
end function; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment