package main
import (
"strings"
"net/http"
"encoding/json"
"fmt"
)
func main() {
j := `{"url":"http://localhost/test/take-request", "params":{"name":"John","age":"20"},"type":"get"}`
// k := `{"url":"http://localhost/test/take-request", "params":{"gender":"m","a":"20"},"type":"post"}`
request := map[string]interface{}{}
err := json.Unmarshal([]byte(j), &request)
if err != nil {
panic(err)
}
fmt.Println(request)
requestType = strings.ToUpper(request["type"]);
requestUrl = request["url"];
fmt.Println(request["params"])
// how do i get the keys and their values from params.
// note params is dynamic.
for _, v := range request["params"].(map[string]interface{}) {
// println(v)
switch t := v.(type) {
case string, []int:
fmt.Println(t)
default:
fmt.Println("wrong type")
}
}
sendRequest(requestType, requestUrl)
}
func sendRequest(type string, url string) string {
req, err := http.NewRequest(type, url, nil)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println("response Body:", string(body))
return string(body)
}
我如何遍历参数是interface
我如何获得密钥及其值
UYOU
相关分类