Skip to content
Snippets Groups Projects
Commit cae9f582 authored by James Page's avatar James Page
Browse files

Merge branch '42-unescape-values-when-splitting-form-data-into-key-value-pairs' into 'main'

Resolve "Unescape values when splitting form data into key-value pairs"

See merge request !55
parents b085bd27 8bd421ec
No related branches found
Tags v1.267.0
1 merge request!55Resolve "Unescape values when splitting form data into key-value pairs"
...@@ -3,6 +3,7 @@ package struct_utils ...@@ -3,6 +3,7 @@ package struct_utils
import ( import (
"github.com/samber/lo" "github.com/samber/lo"
"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/errors" "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/errors"
"net/url"
"reflect" "reflect"
"strings" "strings"
) )
...@@ -15,7 +16,8 @@ type KeyValuePair struct { ...@@ -15,7 +16,8 @@ type KeyValuePair struct {
// FormToKeyValuePairs returns a string-based map of strings as derived from posted form keys and values. // FormToKeyValuePairs returns a string-based map of strings as derived from posted form keys and values.
// e.g. oauth_consumer_key=mlhgs&oauth_consumer_secret=x240ar&oauth_verifier=b0qjbx&store_base_url=http%3A%2F%2Flocalhost.com%2Fstore // e.g. oauth_consumer_key=mlhgs&oauth_consumer_secret=x240ar&oauth_verifier=b0qjbx&store_base_url=http%3A%2F%2Flocalhost.com%2Fstore
func FormToKeyValuePairs(body string) []KeyValuePair { // If values are URL encoded, set doUnescapeValues to true.
func FormToKeyValuePairs(body string, doUnescapeValues bool) ([]KeyValuePair, error) {
out := []KeyValuePair{} out := []KeyValuePair{}
parts := strings.Split(body, "&") parts := strings.Split(body, "&")
for _, p := range parts { for _, p := range parts {
...@@ -27,8 +29,16 @@ func FormToKeyValuePairs(body string) []KeyValuePair { ...@@ -27,8 +29,16 @@ func FormToKeyValuePairs(body string) []KeyValuePair {
} }
var value string var value string
var err error
if len(split) > 1 { if len(split) > 1 {
value = split[1] if doUnescapeValues {
value, err = url.QueryUnescape(split[1])
if err != nil {
return nil, err
}
} else {
value = split[1]
}
} }
kv := KeyValuePair{ kv := KeyValuePair{
...@@ -38,7 +48,7 @@ func FormToKeyValuePairs(body string) []KeyValuePair { ...@@ -38,7 +48,7 @@ func FormToKeyValuePairs(body string) []KeyValuePair {
out = append(out, kv) out = append(out, kv)
} }
return out return out, nil
} }
// GetValue returns the value for the given key from a KeyValuePair slice. // GetValue returns the value for the given key from a KeyValuePair slice.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment