使用strings.NewReader发送包含转义序列的POST请求。

huangapple go评论73阅读模式
英文:

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函数中,userEmailuserToken变量实际上从未被使用过。

你可以使用标准库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)

huangapple
  • 本文由 发表于 2022年9月13日 11:48:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/73697349.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定