net.http: make parse_form return POST requests with json data as a map with a `json` key, containing the json payload (#14289)
parent
87de6df0e6
commit
ce99a306c0
|
@ -244,8 +244,13 @@ fn parse_request_line(s string) ?(Method, urllib.URL, Version) {
|
|||
//
|
||||
// a possible solution is to use the a list of QueryValue
|
||||
pub fn parse_form(body string) map[string]string {
|
||||
words := body.split('&')
|
||||
mut form := map[string]string{}
|
||||
|
||||
if body.match_glob('{*}') {
|
||||
form['json'] = body
|
||||
} else {
|
||||
words := body.split('&')
|
||||
|
||||
for word in words {
|
||||
kv := word.split_nth('=', 2)
|
||||
if kv.len != 2 {
|
||||
|
@ -255,6 +260,7 @@ pub fn parse_form(body string) map[string]string {
|
|||
val := urllib.query_unescape(kv[1]) or { continue }
|
||||
form[key] = val
|
||||
}
|
||||
}
|
||||
return form
|
||||
// }
|
||||
// todo: parse form-data and application/json
|
||||
|
|
|
@ -95,6 +95,30 @@ fn test_parse_form() {
|
|||
'a': 'b'
|
||||
'c': ' d '
|
||||
}
|
||||
assert parse_form('{json}') == {
|
||||
'json': '{json}'
|
||||
}
|
||||
assert parse_form('{
|
||||
"_id": "76c",
|
||||
"friends": [
|
||||
{
|
||||
"id": 0,
|
||||
"name": "Mason Luna"
|
||||
}
|
||||
],
|
||||
"greeting": "Hello."
|
||||
}') == {
|
||||
'json': '{
|
||||
"_id": "76c",
|
||||
"friends": [
|
||||
{
|
||||
"id": 0,
|
||||
"name": "Mason Luna"
|
||||
}
|
||||
],
|
||||
"greeting": "Hello."
|
||||
}'
|
||||
}
|
||||
}
|
||||
|
||||
fn test_parse_multipart_form() {
|
||||
|
|
Loading…
Reference in New Issue