我正在嘗試從接受有效負載的 POST 端點檢索響應。對于curl要求:curl --request POST \ --url https://api.io/v1/oauth/token \ --header 'Accept: application/json' \ --header 'Content-Type: application/json' \ --data '{ "userToken": "[email protected]:MyUserProfileToken" }'我可以這樣做:func GetJWT() string { endpoint := "https://api.io/v1/oauth/token" payload := strings.NewReader(`{ "userToken":"[email protected]:MyUserProfileToken"}`) req, _ := http.NewRequest("POST", endpoint, payload) req.Header.Add("Accept", "application/json") req.Header.Add("Content-Type", "application/json") res, _ := http.DefaultClient.Do(req) defer res.Body.Close() body, _ := ioutil.ReadAll(res.Body) return string(body)}和 payload := strings.NewReader("{\n \"userToken\": \"[email protected]:MyUserProfileToken\"\n}")但是,當我嘗試為電子郵件和令牌傳遞字符串指針,并聲明有效負載時func GetJWT(userEmail, userToken *string) string { endpoint := "https://api.io/v1/oauth/token" payload := strings.NewReader("{\n \"userToken\": \*userEmail\":\"\*userToken\n}") req, _ := http.NewRequest("POST", endpoint, payload) req.Header.Add("Accept", "application/json") req.Header.Add("Content-Type", "application/json") res, _ := http.DefaultClient.Do(req) defer res.Body.Close() body, _ := ioutil.ReadAll(res.Body) return string(body)}未知轉義返回錯誤(負載聲明的第 53 列)。我如何轉義字符串指針以便連接userEmail、“:” 和userToken
1 回答

拉丁的傳說
TA貢獻1789條經驗 獲得超8個贊
我在這里看到幾個問題。
第一:我認為“未知轉義”錯誤消息是由\*
since\*
不是合法的轉義字符引起的。
第二:Golang不支持字符串插值。所以userEmail
和userToken
變量實際上從未在您的GetJWT
函數中使用過。
Sprintf
您可以使用標準庫包將變量格式化為字符串fmt
。那看起來像這樣:
fmt.Sprintf("{\n \"userToken\" : \"%s:%s\" \n}", *userEmail, *userToken)
- 1 回答
- 0 關注
- 175 瀏覽
添加回答
舉報
0/150
提交
取消