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,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

View File

@ -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() {