1
0
mirror of https://github.com/pocketbase/pocketbase.git synced 2025-03-24 23:31:54 +02:00

[#3058] soft-deprecated 'data' prop in favour of 'body' to allow raw strings

This commit is contained in:
Gani Georgiev 2023-08-03 12:31:50 +03:00
parent b3f09ff045
commit b1093baef7
5 changed files with 2665 additions and 2646 deletions

View File

@ -1,3 +1,10 @@
## v0.17.2
- Soft-deprecated `$http.send({ data: object, ... })` in favour of `$http.send({ body: rawString, ... })`
to allow sending non-JSON body with the request ([#3058](https://github.com/pocketbase/pocketbase/discussions/3058)).
The existing `data` prop will still work, but it will be recommended to use `body` instead (_to send JSON you can use `JSON.stringify(...)` as body value_).
## v0.17.1 ## v0.17.1
- Use relative path when redirecting to the OAuth2 providers page in the Admin UI to support subpath deployments ([#3026](https://github.com/pocketbase/pocketbase/pull/3026); thanks @sonyarianto). - Use relative path when redirecting to the OAuth2 providers page in the Admin UI to support subpath deployments ([#3026](https://github.com/pocketbase/pocketbase/pull/3026); thanks @sonyarianto).

View File

@ -578,9 +578,10 @@ func httpClientBinds(vm *goja.Runtime) {
type sendConfig struct { type sendConfig struct {
Method string Method string
Url string Url string
Data map[string]any Body string
Headers map[string]string Headers map[string]string
Timeout int // seconds (default to 120) Timeout int // seconds (default to 120)
Data map[string]any // deprecated, consider using Body instead
} }
obj.Set("send", func(params map[string]any) (*sendResult, error) { obj.Set("send", func(params map[string]any) (*sendResult, error) {
@ -604,6 +605,8 @@ func httpClientBinds(vm *goja.Runtime) {
defer cancel() defer cancel()
var reqBody io.Reader var reqBody io.Reader
// legacy json body data
if len(config.Data) != 0 { if len(config.Data) != 0 {
encoded, err := json.Marshal(config.Data) encoded, err := json.Marshal(config.Data)
if err != nil { if err != nil {
@ -612,6 +615,10 @@ func httpClientBinds(vm *goja.Runtime) {
reqBody = bytes.NewReader(encoded) reqBody = bytes.NewReader(encoded)
} }
if config.Body != "" {
reqBody = strings.NewReader(config.Body)
}
req, err := http.NewRequestWithContext(ctx, strings.ToUpper(config.Method), config.Url, reqBody) req, err := http.NewRequestWithContext(ctx, strings.ToUpper(config.Method), config.Url, reqBody)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -921,8 +921,6 @@ func TestHttpClientBindsSend(t *testing.T) {
bodyRaw, _ := io.ReadAll(req.Body) bodyRaw, _ := io.ReadAll(req.Body)
defer req.Body.Close() defer req.Body.Close()
body := map[string]any{}
json.Unmarshal(bodyRaw, &body)
// normalize headers // normalize headers
headers := make(map[string]string, len(req.Header)) headers := make(map[string]string, len(req.Header))
@ -935,7 +933,7 @@ func TestHttpClientBindsSend(t *testing.T) {
info := map[string]any{ info := map[string]any{
"method": req.Method, "method": req.Method,
"headers": headers, "headers": headers,
"body": body, "body": string(bodyRaw),
} }
infoRaw, _ := json.Marshal(info) infoRaw, _ := json.Marshal(info)
@ -992,8 +990,8 @@ func TestHttpClientBindsSend(t *testing.T) {
const test1 = $http.send({ const test1 = $http.send({
method: "post", method: "post",
url: testUrl, url: testUrl,
data: {"data": "example"},
headers: {"header1": "123", "header2": "456"}, headers: {"header1": "123", "header2": "456"},
body: '789',
}) })
// with custom content-type header // with custom content-type header
@ -1012,6 +1010,7 @@ func TestHttpClientBindsSend(t *testing.T) {
"json.headers.header1": "123", "json.headers.header1": "123",
"json.headers.header2": "456", "json.headers.header2": "456",
"json.headers.content_type": "application/json", // default "json.headers.content_type": "application/json", // default
"json.body": "789",
}], }],
[test2, { [test2, {
"statusCode": "200", "statusCode": "200",

File diff suppressed because it is too large Load Diff

View File

@ -863,11 +863,14 @@ declare namespace $http {
* ` + "```" + ` * ` + "```" + `
*/ */
function send(config: { function send(config: {
url: string, url: string,
body?: string,
method?: string, // default to "GET" method?: string, // default to "GET"
data?: { [key:string]: any },
headers?: { [key:string]: string }, headers?: { [key:string]: string },
timeout?: number // default to 120 timeout?: number, // default to 120
// deprecated, please use body instead
data?: { [key:string]: any },
}): { }): {
statusCode: number statusCode: number
raw: string raw: string