mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
net.http: Response.text
-> Response.body
(#14478)
This commit is contained in:
@ -16,9 +16,9 @@ pub fn download_file(url string, out_file_path string) ? {
|
||||
return error('received http code $s.status_code')
|
||||
}
|
||||
$if debug_http ? {
|
||||
println('http.download_file saving $s.text.len bytes')
|
||||
println('http.download_file saving $s.body.len bytes')
|
||||
}
|
||||
os.write_file(out_file_path, s.text)?
|
||||
os.write_file(out_file_path, s.body)?
|
||||
}
|
||||
|
||||
// TODO: implement download_file_with_progress
|
||||
|
@ -161,7 +161,7 @@ pub fn fetch(config FetchConfig) ?Response {
|
||||
// get_text sends a GET HTTP request to the URL and returns the text content of the response
|
||||
pub fn get_text(url string) string {
|
||||
resp := fetch(url: url, method: .get) or { return '' }
|
||||
return resp.text
|
||||
return resp.body
|
||||
}
|
||||
|
||||
// url_encode_form_data converts mapped data to an URL encoded string
|
||||
|
@ -25,7 +25,7 @@ fn http_fetch_mock(_methods []string, _config FetchConfig) ?[]Response {
|
||||
config.method = method_from_str(method)
|
||||
res := fetch(FetchConfig{ ...config, url: url + lmethod })?
|
||||
// TODO
|
||||
// body := json.decode(HttpbinResponseBody,res.text)?
|
||||
// body := json.decode(HttpbinResponseBody,res.body)?
|
||||
result << res
|
||||
}
|
||||
return result
|
||||
@ -49,7 +49,7 @@ fn test_http_fetch_with_data() {
|
||||
data: 'hello world'
|
||||
) or { panic(err) }
|
||||
for response in responses {
|
||||
payload := json.decode(HttpbinResponseBody, response.text) or { panic(err) }
|
||||
payload := json.decode(HttpbinResponseBody, response.body) or { panic(err) }
|
||||
assert payload.data == 'hello world'
|
||||
}
|
||||
}
|
||||
@ -65,7 +65,7 @@ fn test_http_fetch_with_params() {
|
||||
}
|
||||
) or { panic(err) }
|
||||
for response in responses {
|
||||
// payload := json.decode(HttpbinResponseBody,response.text) or {
|
||||
// payload := json.decode(HttpbinResponseBody,response.body) or {
|
||||
// panic(err)
|
||||
// }
|
||||
assert response.status() == .ok
|
||||
@ -85,7 +85,7 @@ fn test_http_fetch_with_headers() ? {
|
||||
header: header
|
||||
) or { panic(err) }
|
||||
for response in responses {
|
||||
// payload := json.decode(HttpbinResponseBody,response.text) or {
|
||||
// payload := json.decode(HttpbinResponseBody,response.body) or {
|
||||
// panic(err)
|
||||
// }
|
||||
assert response.status() == .ok
|
||||
|
@ -17,9 +17,9 @@ fn test_http_get_from_vlang_utc_now() {
|
||||
println('Test getting current time from $url by http.get')
|
||||
res := http.get(url) or { panic(err) }
|
||||
assert res.status() == .ok
|
||||
assert res.text.len > 0
|
||||
assert res.text.int() > 1566403696
|
||||
println('Current time is: $res.text.int()')
|
||||
assert res.body.len > 0
|
||||
assert res.body.int() > 1566403696
|
||||
println('Current time is: $res.body.int()')
|
||||
}
|
||||
}
|
||||
|
||||
@ -39,7 +39,7 @@ fn test_public_servers() {
|
||||
println('Testing http.get on public url: $url ')
|
||||
res := http.get(url) or { panic(err) }
|
||||
assert res.status() == .ok
|
||||
assert res.text.len > 0
|
||||
assert res.body.len > 0
|
||||
}
|
||||
}
|
||||
|
||||
@ -51,6 +51,6 @@ fn test_relative_redirects() {
|
||||
} // tempfix periodic: httpbin relative redirects are broken
|
||||
res := http.get('https://httpbin.org/relative-redirect/3?abc=xyz') or { panic(err) }
|
||||
assert res.status() == .ok
|
||||
assert res.text.len > 0
|
||||
assert res.text.contains('"abc": "xyz"')
|
||||
assert res.body.len > 0
|
||||
assert res.body.contains('"abc": "xyz"')
|
||||
}
|
||||
|
@ -9,7 +9,8 @@ import strconv
|
||||
// Response represents the result of the request
|
||||
pub struct Response {
|
||||
pub mut:
|
||||
text string
|
||||
body string
|
||||
text string [deprecated: 'use Response.body instead'; deprecated_after: '2022-10-03']
|
||||
header Header
|
||||
status_code int
|
||||
status_msg string
|
||||
@ -30,7 +31,7 @@ pub fn (resp Response) bytes() []u8 {
|
||||
pub fn (resp Response) bytestr() string {
|
||||
return 'HTTP/$resp.http_version $resp.status_code $resp.status_msg\r\n' + '${resp.header.render(
|
||||
version: resp.version()
|
||||
)}\r\n' + '$resp.text'
|
||||
)}\r\n' + '$resp.body'
|
||||
}
|
||||
|
||||
// Parse a raw HTTP response into a Response object
|
||||
@ -39,16 +40,17 @@ pub fn parse_response(resp string) ?Response {
|
||||
// Build resp header map and separate the body
|
||||
start_idx, end_idx := find_headers_range(resp)?
|
||||
header := parse_headers(resp.substr(start_idx, end_idx))?
|
||||
mut text := resp.substr(end_idx, resp.len)
|
||||
mut body := resp.substr(end_idx, resp.len)
|
||||
if header.get(.transfer_encoding) or { '' } == 'chunked' {
|
||||
text = chunked.decode(text)
|
||||
body = chunked.decode(body)
|
||||
}
|
||||
return Response{
|
||||
http_version: version
|
||||
status_code: status_code
|
||||
status_msg: status_msg
|
||||
header: header
|
||||
text: text
|
||||
body: body
|
||||
text: body // TODO: remove as depreciated
|
||||
}
|
||||
}
|
||||
|
||||
@ -113,18 +115,19 @@ pub struct ResponseConfig {
|
||||
version Version = .v1_1
|
||||
status Status = .ok
|
||||
header Header
|
||||
text string
|
||||
body string
|
||||
text string [deprecated: 'use ResponseConfig.body instead'; deprecated_after: '2022-10-03']
|
||||
}
|
||||
|
||||
// new_response creates a Response object from the configuration. This
|
||||
// function will add a Content-Length header if text is not empty.
|
||||
// function will add a Content-Length header if body is not empty.
|
||||
pub fn new_response(conf ResponseConfig) Response {
|
||||
mut resp := Response{
|
||||
text: conf.text
|
||||
body: conf.body + conf.text
|
||||
header: conf.header
|
||||
}
|
||||
if conf.text.len > 0 && !resp.header.contains(.content_length) {
|
||||
resp.header.add(.content_length, conf.text.len.str())
|
||||
if resp.body.len > 0 && !resp.header.contains(.content_length) {
|
||||
resp.header.add(.content_length, resp.body.len.str())
|
||||
}
|
||||
resp.set_status(conf.status)
|
||||
resp.set_version(conf.version)
|
||||
|
@ -4,14 +4,14 @@ fn test_response_bytestr() ? {
|
||||
{
|
||||
resp := new_response(
|
||||
status: .ok
|
||||
text: 'Foo'
|
||||
text: 'Foo' // TODO: replace with `body` once deprecaped
|
||||
)
|
||||
assert resp.bytestr() == 'HTTP/1.1 200 OK\r\n' + 'Content-Length: 3\r\n' + '\r\n' + 'Foo'
|
||||
}
|
||||
{
|
||||
resp := new_response(
|
||||
status: .found
|
||||
text: 'Foo'
|
||||
body: 'Foo'
|
||||
header: new_header(key: .location, value: '/')
|
||||
)
|
||||
lines := resp.bytestr().split_into_lines()
|
||||
|
@ -115,7 +115,7 @@ fn (d DebugHandler) handle(req Request) Response {
|
||||
eprintln('[$time.now()] $req.method $req.url - 200')
|
||||
}
|
||||
mut r := Response{
|
||||
text: req.data
|
||||
body: req.data
|
||||
header: req.header
|
||||
}
|
||||
r.set_status(.ok)
|
||||
|
@ -41,7 +41,7 @@ fn (mut handler MyHttpHandler) handle(req http.Request) http.Response {
|
||||
handler.counter++
|
||||
// eprintln('$time.now() | counter: $handler.counter | $req.method $req.url\n$req.header\n$req.data - 200 OK\n')
|
||||
mut r := http.Response{
|
||||
text: req.data + ', $req.url'
|
||||
body: req.data + ', $req.url'
|
||||
header: req.header
|
||||
}
|
||||
match req.url.all_before('?') {
|
||||
@ -72,11 +72,11 @@ fn test_server_custom_handler() ? {
|
||||
time.sleep(10 * time.millisecond)
|
||||
}
|
||||
x := http.fetch(url: 'http://localhost:$cport/endpoint?abc=xyz', data: 'my data')?
|
||||
assert x.text == 'my data, /endpoint?abc=xyz'
|
||||
assert x.body == 'my data, /endpoint?abc=xyz'
|
||||
assert x.status_code == 200
|
||||
assert x.http_version == '1.1'
|
||||
y := http.fetch(url: 'http://localhost:$cport/another/endpoint', data: 'abcde')?
|
||||
assert y.text == 'abcde, /another/endpoint'
|
||||
assert y.body == 'abcde, /another/endpoint'
|
||||
assert y.status_code == 200
|
||||
assert y.status() == .ok
|
||||
assert y.http_version == '1.1'
|
||||
|
Reference in New Issue
Block a user