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,16 +244,22 @@ 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{}
|
||||
for word in words {
|
||||
kv := word.split_nth('=', 2)
|
||||
if kv.len != 2 {
|
||||
continue
|
||||
|
||||
if body.match_glob('{*}') {
|
||||
form['json'] = body
|
||||
} else {
|
||||
words := body.split('&')
|
||||
|
||||
for word in words {
|
||||
kv := word.split_nth('=', 2)
|
||||
if kv.len != 2 {
|
||||
continue
|
||||
}
|
||||
key := urllib.query_unescape(kv[0]) or { continue }
|
||||
val := urllib.query_unescape(kv[1]) or { continue }
|
||||
form[key] = val
|
||||
}
|
||||
key := urllib.query_unescape(kv[0]) or { continue }
|
||||
val := urllib.query_unescape(kv[1]) or { continue }
|
||||
form[key] = val
|
||||
}
|
||||
return form
|
||||
// }
|
||||
|
|
|
@ -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