net.http: make parse_form return POST requests with json data as a map with a `json` key, containing the json payload (#14289)

Lathanao 2022-05-06 17:23:36 +00:00 committed by Jef Roosens
parent 5344215e96
commit 57b490706b
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
2 changed files with 38 additions and 8 deletions

View File

@ -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 // a possible solution is to use the a list of QueryValue
pub fn parse_form(body string) map[string]string { pub fn parse_form(body string) map[string]string {
words := body.split('&')
mut form := map[string]string{} mut form := map[string]string{}
for word in words {
kv := word.split_nth('=', 2) if body.match_glob('{*}') {
if kv.len != 2 { form['json'] = body
continue } 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 return form
// } // }

View File

@ -95,6 +95,30 @@ fn test_parse_form() {
'a': 'b' 'a': 'b'
'c': ' d ' '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() { fn test_parse_multipart_form() {