diff --git a/docs/plugins/library/http.md b/docs/plugins/library/http.md index 5fa36b8f..6fdbd114 100644 --- a/docs/plugins/library/http.md +++ b/docs/plugins/library/http.md @@ -4,11 +4,12 @@ use `require("http")` to access it. For example: **Usage** + ```lua local http = require("http") --- get request, do not use this request to download files!!! local resp, err = http.get({ - url = "http://ip.jsontest.com/", + url = "https://httpbin.org/json", headers = { ['Host'] = "localhost" } @@ -17,11 +18,11 @@ local resp, err = http.get({ assert(err == nil) assert(resp.status_code == 200) assert(resp.headers['Content-Type'] == 'application/json') -assert(resp.body == '{"ip": "xxx.xxx.xxx.xxx"}') +assert(resp.body == 'xxxxx') --- head request resp, err = http.head({ - url = "http://ip.jsontest.com/", + url = "https://httpbin.org/json", headers = { ['Host'] = "localhost" } @@ -37,4 +38,4 @@ err = http.download_file({ }, "/usr/local/file") assert(err == nil, [[must be nil]] ) -``` \ No newline at end of file +``` diff --git a/docs/zh-hans/plugins/library/http.md b/docs/zh-hans/plugins/library/http.md index 08e825a9..2cc2024f 100644 --- a/docs/zh-hans/plugins/library/http.md +++ b/docs/zh-hans/plugins/library/http.md @@ -1,14 +1,14 @@ -# Http标准库 - -`vfox`提供了一个简单的http能力,当前支持`Get`、`Head`两种请求类型,以及文件下载。 +# Http 标准库 +`vfox`提供了一个简单的 http 能力,当前支持`Get`、`Head`两种请求类型,以及文件下载。 **使用** + ```lua local http = require("http") --- get 请求, 不要用此请求进行文件下载!!! local resp, err = http.get({ - url = "http://ip.jsontest.com/", + url = "https://httpbin.org/json", headers = { ['Host'] = "localhost" } @@ -17,11 +17,11 @@ local resp, err = http.get({ assert(err == nil) assert(resp.status_code == 200) assert(resp.headers['Content-Type'] == 'application/json') -assert(resp.body == '{"ip": "xxx.xxx.xxx.xxx"}') +assert(resp.body == 'xxxxxxxx') ---- head 请求 +--- head 请求 resp, err = http.head({ - url = "http://ip.jsontest.com/", + url = "https://httpbin.org/json", headers = { ['Host'] = "localhost" } @@ -37,4 +37,4 @@ err = http.download_file({ }, "/usr/local/file") assert(err == nil, [[must be nil]] ) -``` \ No newline at end of file +``` diff --git a/internal/module/http/http.go b/internal/module/http/http.go index 4d9bd376..cf3bdad9 100644 --- a/internal/module/http/http.go +++ b/internal/module/http/http.go @@ -18,14 +18,15 @@ package http import ( "fmt" - "github.com/schollz/progressbar/v3" - "github.com/version-fox/vfox/internal/config" - lua "github.com/yuin/gopher-lua" "io" "net/http" "net/url" "os" "path/filepath" + + "github.com/schollz/progressbar/v3" + "github.com/version-fox/vfox/internal/config" + lua "github.com/yuin/gopher-lua" ) type Module struct { @@ -41,7 +42,7 @@ type Module struct { // local http = require("http") // // http.get({ -// url = "http://ip.jsontest.com/" +// url = "https://httpbin.org/json" // }) return (response, error) // // response : { diff --git a/internal/module/http/http_test.go b/internal/module/http/http_test.go index 81903bce..6f8b5893 100644 --- a/internal/module/http/http_test.go +++ b/internal/module/http/http_test.go @@ -17,11 +17,12 @@ package http import ( - "github.com/version-fox/vfox/internal/util" "os" "runtime" "testing" + "github.com/version-fox/vfox/internal/util" + "github.com/version-fox/vfox/internal/config" lua "github.com/yuin/gopher-lua" ) @@ -36,10 +37,10 @@ func TestWithConfig(t *testing.T) { assert(type(http) == "table") assert(type(http.get) == "function") local resp, err = http.get({ - url = "http://ip.jsontest.com/" + url = "https://httpbin.org/json" }) print(err) - assert(err == 'Get "http://ip.jsontest.com/": proxyconnect tcp: dial tcp 127.0.0.1:80: connect: connection refused') + assert(err == 'Get "https://httpbin.org/json": proxyconnect tcp: dial tcp 127.0.0.1:80: connect: connection refused') ` s := lua.NewState() defer s.Close() @@ -58,7 +59,7 @@ func TestGetRequest(t *testing.T) { assert(type(http) == "table") assert(type(http.get) == "function") local resp, err = http.get({ - url = "http://ip.jsontest.com/" + url = "https://httpbin.org/json" }) assert(err == nil) assert(resp.status_code == 200) @@ -73,7 +74,7 @@ func TestHeadRequest(t *testing.T) { assert(type(http) == "table") assert(type(http.get) == "function") local resp, err = http.head({ - url = "http://ip.jsontest.com/" + url = "https://httpbin.org/json" }) assert(err == nil) assert(resp.status_code == 200)