8.16.0.1
4.2 网络相关标准库
(require azelf/std/http) | package: azelf |
该模块自动导出了net/url,算是对原有功能的增强。
4.2.1 扩展接口
procedure
(url/clear-pathname url-link) → (Array/c string?)
url-link : url?
给出干净的pathname。
Examples:
> (require azelf/std/http) > (url/clear-pathname (string->url "http://baidu.com/a;c/b;d/image.jpg")) (Array "a" "b" "image.jpg")
procedure
(url/filename url-link) → (Maybe/c string?)
url-link : url? (url/filename-without-ext url-link) → (Maybe/c string?) url-link : url?
Examples:
> (require azelf/std/http) > (url/filename (string->url "http://www.baidu.com")) (Nothing)
> (url/filename (string->url "http://www.baidu.com/a/b.mp3")) (Just "b.mp3")
> (url/filename (string->url "http://www.baidu.com/")) (Just "")
为链接重命名,此处用的是高阶函数f,避免手动再获取一次。
(define url-link (string->url "http://baidu.com/a/b/c.jpg")) (url/rename-with string-upcase url-link) ; http://baidu.com/a/b/C.jpg
procedure
(url/rename-to filename url-link) → url?
filename : string? url-link : url?
Example:
> (url/rename-to "404.gif" (string->url "http://www.baidu.com/file.html")) (url "http" #f "www.baidu.com" #f #t (list (path/param "404.gif" '())) '() #f)
4.2.2 HTTP客户端
创建一个简易的客户端实现,目前只返回响应体,没有响应头。因为发送请求不算是个纯函数,所以下面只列出必要的函数说明。
procedure
(http/get option) → input-port?
option : Requestable? (http/head option) → input-port? option : Requestable? (http/delete option) → input-port? option : Requestable? (http/options option) → input-port? option : Requestable? (http/get/json option) → any/c option : Requestable? (http/head/json option) → any/c option : Requestable? (http/delete/json option) → any/c option : Requestable? (http/options/json option) → any/c option : Requestable? (http/get/html option) → string? option : Requestable? (http/head/html option) → string? option : Requestable? (http/delete/html option) → string? option : Requestable? (http/options/html option) → string? option : Requestable? (http/post option) → input-port? option : ToBodyRequest? (http/put option) → input-port? option : ToBodyRequest? (http/post/json option) → any/c option : ToBodyRequest? (http/put/json option) → any/c option : ToBodyRequest? (http/post/html option) → string? option : ToBodyRequest? (http/put/html option) → string? option : ToBodyRequest?
发送请求。这些方法之间,仅method和返回数据结构的不同。
(http/get "www.baidu.com") (http/post/json "www.baidu.com") (http/delete (string->url "www.google.com"))
procedure
(http/set-query key value option) → RequestOption?
key : symbol? value : Show? option : Requestable? (http/set-header key value option) → RequestOption? key : Show? value : Show? option : Requestable? (http/set-redirect redirect option) → RequestOption? redirect : exact-nonnegative-integer? option : Requestable? (http/set-json body option) → RequestOption? body : ToJSON? option : Requestable? (http/set-body body option) → RequestOption? body : bytes? option : Requestable?
一些常规的请求设定,字面意思。
(->> "http://localhost" (http/set-header "Auth" "yes") (http/set-json (hashmap 'a "b" 'c "d")) (http/post))
procedure
(http/download save-path link) → void?
save-path : path-string? link : Requestable?
保存网络资源到本地。