[#6282] soft-deprecated $http.send result.raw field in favour of result.body

This commit is contained in:
Gani Georgiev 2025-04-04 11:32:47 +03:00
parent 409bcdaa96
commit 9f6010d38d
6 changed files with 3100 additions and 2909 deletions

View File

@ -8,6 +8,9 @@
- Forced `text/javascript` Content-Type when serving `.js`/`.mjs` collection uploaded files ([#6597](https://github.com/pocketbase/pocketbase/issues/6597)). - Forced `text/javascript` Content-Type when serving `.js`/`.mjs` collection uploaded files ([#6597](https://github.com/pocketbase/pocketbase/issues/6597)).
- Soft-deprecated the `$http.send`'s `result.raw` field in favour of `result.body` that contains the response body as plain bytes slice to avoid the discrepencies between Go and the JSVM when casting binary data to string.
(@todo update docs to use the new field)
- Minor UI fixes (_removed the superuser fields from the auth record create/update body examples, etc._). - Minor UI fixes (_removed the superuser fields from the auth record create/update body examples, etc._).

View File

@ -774,11 +774,15 @@ func httpClientBinds(vm *goja.Runtime) {
}) })
type sendResult struct { type sendResult struct {
JSON any `json:"json"` JSON any `json:"json"`
Headers map[string][]string `json:"headers"` Headers map[string][]string `json:"headers"`
Cookies map[string]*http.Cookie `json:"cookies"` Cookies map[string]*http.Cookie `json:"cookies"`
Raw string `json:"raw"`
StatusCode int `json:"statusCode"` // Deprecated: consider using Body instead
Raw string `json:"raw"`
Body []byte `json:"body"`
StatusCode int `json:"statusCode"`
} }
type sendConfig struct { type sendConfig struct {
@ -883,6 +887,7 @@ func httpClientBinds(vm *goja.Runtime) {
Headers: map[string][]string{}, Headers: map[string][]string{},
Cookies: map[string]*http.Cookie{}, Cookies: map[string]*http.Cookie{},
Raw: string(bodyRaw), Raw: string(bodyRaw),
Body: bodyRaw,
} }
for k, v := range res.Header { for k, v := range res.Header {
@ -893,7 +898,7 @@ func httpClientBinds(vm *goja.Runtime) {
result.Cookies[v.Name] = v result.Cookies[v.Name] = v
} }
if len(result.Raw) != 0 { if len(result.Body) > 0 {
// try as map // try as map
result.JSON = map[string]any{} result.JSON = map[string]any{}
if err := json.Unmarshal(bodyRaw, &result.JSON); err != nil { if err := json.Unmarshal(bodyRaw, &result.JSON); err != nil {

View File

@ -1363,6 +1363,13 @@ func TestHttpClientBindsSend(t *testing.T) {
headers: {"content-type": "text/plain"}, // should be ignored headers: {"content-type": "text/plain"}, // should be ignored
}) })
// raw body response field check
const test4 = $http.send({
method: "post",
url: testURL,
body: 'test',
})
const scenarios = [ const scenarios = [
[test0, { [test0, {
"statusCode": "400", "statusCode": "400",
@ -1395,6 +1402,13 @@ func TestHttpClientBindsSend(t *testing.T) {
"multipart/form-data; boundary=" "multipart/form-data; boundary="
], ],
}], }],
[test4, {
"statusCode": "200",
"headers.X-Custom.0": "custom_header",
"cookies.sessionId.value": "123456",
// {"body":"test","headers":{"accept_encoding":"gzip","content_length":"4","user_agent":"Go-http-client/1.1"},"method":"POST"}
"body": [123,34,98,111,100,121,34,58,34,116,101,115,116,34,44,34,104,101,97,100,101,114,115,34,58,123,34,97,99,99,101,112,116,95,101,110,99,111,100,105,110,103,34,58,34,103,122,105,112,34,44,34,99,111,110,116,101,110,116,95,108,101,110,103,116,104,34,58,34,52,34,44,34,117,115,101,114,95,97,103,101,110,116,34,58,34,71,111,45,104,116,116,112,45,99,108,105,101,110,116,47,49,46,49,34,125,44,34,109,101,116,104,111,100,34,58,34,80,79,83,84,34,125],
}],
] ]
for (let scenario of scenarios) { for (let scenario of scenarios) {
@ -1408,13 +1422,13 @@ func TestHttpClientBindsSend(t *testing.T) {
// check for partial match(es) // check for partial match(es)
for (let exp of expectation) { for (let exp of expectation) {
if (!value.includes(exp)) { if (!value.includes(exp)) {
throw new Error('Expected ' + key + ' to contain ' + exp + ', got: ' + result.raw); throw new Error('Expected ' + key + ' to contain ' + exp + ', got: ' + toString(result.body));
} }
} }
} else { } else {
// check for direct match // check for direct match
if (value != expectation) { if (value != expectation) {
throw new Error('Expected ' + key + ' ' + expectation + ', got: ' + result.raw); throw new Error('Expected ' + key + ' ' + expectation + ', got: ' + toString(result.body));
} }
} }
} }

View File

@ -126,7 +126,8 @@ func (data FormData) toMultipart() (*bytes.Buffer, *multipart.Writer, error) {
} }
defer file.Close() defer file.Close()
if _, err := io.Copy(mpw, file); err != nil { _, err = io.Copy(mpw, file)
if err != nil {
return err return err
} }
@ -136,7 +137,8 @@ func (data FormData) toMultipart() (*bytes.Buffer, *multipart.Writer, error) {
return nil, nil, err return nil, nil, err
} }
default: default:
if err := mp.WriteField(k, cast.ToString(v)); err != nil { err := mp.WriteField(k, cast.ToString(v))
if err != nil {
return nil, nil, err return nil, nil, err
} }
} }

File diff suppressed because it is too large Load Diff

View File

@ -1077,7 +1077,7 @@ declare namespace $http {
* console.log(res.statusCode) // the response HTTP status code * console.log(res.statusCode) // the response HTTP status code
* console.log(res.headers) // the response headers (eg. res.headers['X-Custom'][0]) * console.log(res.headers) // the response headers (eg. res.headers['X-Custom'][0])
* console.log(res.cookies) // the response cookies (eg. res.cookies.sessionId.value) * console.log(res.cookies) // the response cookies (eg. res.cookies.sessionId.value)
* console.log(res.raw) // the response body as plain text * console.log(res.body) // the response body as raw bytes slice
* console.log(res.json) // the response body as parsed json array or map * console.log(res.json) // the response body as parsed json array or map
* ` + "```" + ` * ` + "```" + `
*/ */
@ -1094,8 +1094,11 @@ declare namespace $http {
statusCode: number, statusCode: number,
headers: { [key:string]: Array<string> }, headers: { [key:string]: Array<string> },
cookies: { [key:string]: http.Cookie }, cookies: { [key:string]: http.Cookie },
raw: string,
json: any, json: any,
body: Array<number>,
// @deprecated please use toString(result.body) instead
raw: string,
}; };
} }