英文:
Post Request with strings.NewReader Containing Escape Sequence
问题
我正在尝试从接受有效载荷的POST端点中获取响应。
对于curl
请求:
curl --request POST \
--url https://api.io/v1/oauth/token \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--data '{
"userToken": "myemail@domain.com:MyUserProfileToken"
}'
我可以使用以下代码实现:
func GetJWT() string {
endpoint := "https://api.io/v1/oauth/token"
payload := strings.NewReader(`{
"userToken":"myemail@domain.com: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(`{
"userToken": "myemail@domain.com:MyUserProfileToken"
}`)
然而,当我尝试传递电子邮件和令牌的字符串指针,并声明如下的有效载荷时:
func GetJWT(userEmail, userToken *string) string {
endpoint := "https://api.io/v1/oauth/token"
payload := strings.NewReader(`{
"userToken":*userEmail:":"*userToken
}`)
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
?
英文:
I'm trying to retrieve a response from a POST endpoint which accepts a payload.
For curl
request:
curl --request POST \
--url https://api.io/v1/oauth/token \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--data '{
"userToken": "myemail@domain.com:MyUserProfileToken"
}'
I can do this with:
func GetJWT() string {
endpoint := "https://api.io/v1/oauth/token"
payload := strings.NewReader(`{
"userToken":"myemail@domain.com: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)
}
and
payload := strings.NewReader("{\n \"userToken\": \"myemail@domain.com:MyUserProfileToken\"\n}")
However, when I try to pass string pointers for email and token, and declare the payload like
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)
}
an error is returned for unknown escape (column 53 on payload declaration).
How can I escape string pointers so that I can concatenate userEmail
, ":", and userToken
答案1
得分: 1
我看到了几个问题。
首先:我认为“unknown escape”错误消息是由于\*
引起的,因为\*
不是一个合法的转义字符。
其次:Golang不支持字符串插值。所以在你的GetJWT
函数中,userEmail
和userToken
变量实际上从未被使用过。
你可以使用标准库fmt
包中的Sprintf
将变量格式化为字符串。代码如下:
fmt.Sprintf("{\n \"userToken\" : \"%s:%s\" \n}", *userEmail, *userToken)
英文:
I see a couple problems here.
First: I think the "unknown escape" error message is caused by \*
since \*
is not a legitimate escape character.
Second: Golang does not support string interpolation. So the userEmail
and userToken
variables are actually never used in your GetJWT
function.
You can format variables into a string using Sprintf
from the standard library fmt
package. That would look like this:
fmt.Sprintf("{\n \"userToken\" : \"%s:%s\" \n}", *userEmail, *userToken)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论