jokes-bapak2/api/utils/parse.go

35 lines
665 B
Go
Raw Normal View History

2021-07-09 12:13:19 +00:00
package utils
import (
"strconv"
"github.com/pquerna/ffjson/ffjson"
2021-07-09 12:13:19 +00:00
)
// ParseToFormBody converts a body to form data type
func ParseToFormBody(body map[string]interface{}) ([]byte, error) {
var form string
for key, value := range body {
form += key + "="
switch v := value.(type) {
case string:
form += v
case int:
form += strconv.Itoa(v)
case bool:
form += strconv.FormatBool(v)
}
form += "&"
}
return []byte(form), nil
}
// ParseToJSONBody converts a body to json data type
func ParseToJSONBody(body map[string]interface{}) ([]byte, error) {
b, err := ffjson.Marshal(body)
2021-07-09 12:13:19 +00:00
if err != nil {
return nil, err
}
return b, nil
}