在发送之前,有没有一种方法可以清除这个POST请求中的反斜杠?

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

Is there a way to clean this POST request of backslashes before I send it off?

问题

我正在编写一个程序,该程序发送一个需要纯文本中的双引号的POST请求。因为每次发送请求时我都使用fmt.Sprintf注入变量,所以在发送请求时我使用字符串字面量。

func searchLocalImage(imageName, imageTag, artifactoryUsername, artifactoryPassword string) (localImageRepo string) {
	a := types.ArtifactoryClient{
		Url:      "<url>",
		Username: artifactoryUsername,
		Password: artifactoryPassword,
		Client:   resty.New(),
	}
	var responseBody map[string]interface{}

	url := a.Url + "/search/aql"
	res, _ := a.Client.R().
		SetHeader("Content-Type", "text/plain").
		SetBasicAuth(a.Username, a.Password).
		SetBody(fmt.Sprintf(`items.find({"$and": [{"repo":{"$match":"<folder>"}},{"path": {"$match":"%s/%s"}},{"name":{"$match":"<tag>"}}]}).include("repo","path","name")`, imageName, imageTag)).
		Post(url)
}

当然,这会自动在返回的字符串中注入转义字符。

"items.find({\"$and\": [{\"repo\":{\"$match\":\"<folder>\"}},{\"path\": {\"$match\":\"<folderPath>\"}},{\"name\":{\"$match\":\"<tag>\"}}]}).include(\"repo\",\"path\",\"name\")"

不幸的是,这会导致响应是一个错误消息。有人找到了一种在不发送这些反斜杠的情况下发送这样的请求的方法吗?

英文:

I'm writing a program that sends a POST request whose body requires double quotes in plaintext. Because I'm injecting variables with fmt.Sprintf everytime I send a request, I'm using string literals when sending the request off

func searchLocalImage(imageName, imageTag, artifactoryUsername, artifactoryPassword string) (localImageRepo string) {
	a := types.ArtifactoryClient{
		Url:      &quot;&lt;url&gt;&quot;,
		Username: artifactoryUsername,
		Password: artifactoryPassword,
		Client:   resty.New(),
	}
	var responseBody map[string]interface{}

	url := a.Url + &quot;/search/aql&quot;
	res, _ := a.Client.R().
		SetHeader(&quot;Content-Type&quot;, &quot;text/plain&quot;).
		SetBasicAuth(a.Username, a.Password).
		SetBody(fmt.Sprintf(`items.find({&quot;$and&quot;: [{&quot;repo&quot;:{&quot;$match&quot;:&quot;&lt;folder&gt;&quot;}},{&quot;path&quot;: {&quot;$match&quot;:&quot;%s/%s&quot;}},{&quot;name&quot;:{&quot;$match&quot;:&quot;&lt;tag&gt;&quot;}}]}).include(&quot;repo&quot;,&quot;path&quot;,&quot;name&quot;)`, imageName, imageTag)).
		Post(url)

Of course, this automatically injects escape characters to returned string.

&quot;items.find({\&quot;$and\&quot;: [{\&quot;repo\&quot;:{\&quot;$match\&quot;:\&quot;&lt;folder&gt;\&quot;}},{\&quot;path\&quot;: {\&quot;$match\&quot;:\&quot;&lt;folderPath&gt;\&quot;}},{\&quot;name\&quot;:{\&quot;$match\&quot;:\&quot;&lt;tag&gt;\&quot;}}]}).include(\&quot;repo\&quot;,\&quot;path\&quot;,\&quot;name\&quot;)&quot;

This unfortunately results in the response being an error message. Has anybody found a way to send a request like this the way I would need to without also sending those backslashes?

答案1

得分: 2

当然,这会自动向返回的字符串中注入转义字符。

不,这是不正确的。你看到转义字符是因为你使用的工具(很可能是调试器)将内容显示为带引号的字符串。

你可以简单地转储请求以查看发送的内容。然后将转储内容粘贴到 Insomnia 中,以找出问题所在。

package main

import (
	"fmt"
	"net/http"
	"net/http/httptest"
	"net/http/httputil"

	"github.com/go-resty/resty/v2"
)

func main() {
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		buf, err := httputil.DumpRequest(r, true)
		if err != nil {
			panic(err)
		}

		fmt.Printf("%s\n", buf)
	}))
	defer ts.Close()

	client := resty.New()

	imageName := "image name"
	imageTag := "image tag"
	_, err := client.R().
		SetHeader("Content-Type", "text/plain").
		SetBasicAuth("<username>", "<password>").
		SetBody(fmt.Sprintf(`items.find({"$and": [{"repo":{"$match":"<folder>"}},{"path": {"$match":"%s/%s"}},{"name":{"$match":"<tag>"}}]}).include("repo","path","name")`, imageName, imageTag)).
		Post(ts.URL)
	if err != nil {
		panic(err)
	}
}
POST / HTTP/1.1
Host: 127.0.0.1:41937
Accept-Encoding: gzip
Authorization: Basic PHVzZXJuYW1lPjo8cGFzc3dvcmQ+
Content-Length: 156
Content-Type: text/plain
User-Agent: go-resty/2.7.0 (https://github.com/go-resty/resty)

items.find({"$and": [{"repo":{"$match":"<folder>"}},{"path": {"$match":"image name/image tag"}},{"name":{"$match":"<tag>"}}]}).include("repo","path","name")
英文:

> Of course, this automatically injects escape characters to returned string.

No, this is not true. You see the escape characters because the tool that you used (most likely a debugger) displays the content as a quoted string.

You can simply dump the request to see what is sent. And paste the dump content into Insomnia to find out what's wrong.

package main

import (
	&quot;fmt&quot;
	&quot;net/http&quot;
	&quot;net/http/httptest&quot;
	&quot;net/http/httputil&quot;

	&quot;github.com/go-resty/resty/v2&quot;
)

func main() {
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		buf, err := httputil.DumpRequest(r, true)
		if err != nil {
			panic(err)
		}

		fmt.Printf(&quot;%s\n&quot;, buf)
	}))
	defer ts.Close()

	client := resty.New()

	imageName := &quot;image name&quot;
	imageTag := &quot;image tag&quot;
	_, err := client.R().
		SetHeader(&quot;Content-Type&quot;, &quot;text/plain&quot;).
		SetBasicAuth(&quot;&lt;username&gt;&quot;, &quot;&lt;password&gt;&quot;).
		SetBody(fmt.Sprintf(`items.find({&quot;$and&quot;: [{&quot;repo&quot;:{&quot;$match&quot;:&quot;&lt;folder&gt;&quot;}},{&quot;path&quot;: {&quot;$match&quot;:&quot;%s/%s&quot;}},{&quot;name&quot;:{&quot;$match&quot;:&quot;&lt;tag&gt;&quot;}}]}).include(&quot;repo&quot;,&quot;path&quot;,&quot;name&quot;)`, imageName, imageTag)).
		Post(ts.URL)
	if err != nil {
		panic(err)
	}
}
POST / HTTP/1.1
Host: 127.0.0.1:41937
Accept-Encoding: gzip
Authorization: Basic PHVzZXJuYW1lPjo8cGFzc3dvcmQ+
Content-Length: 156
Content-Type: text/plain
User-Agent: go-resty/2.7.0 (https://github.com/go-resty/resty)
items.find({&quot;$and&quot;: [{&quot;repo&quot;:{&quot;$match&quot;:&quot;&lt;folder&gt;&quot;}},{&quot;path&quot;: {&quot;$match&quot;:&quot;image name/image tag&quot;}},{&quot;name&quot;:{&quot;$match&quot;:&quot;&lt;tag&gt;&quot;}}]}).include(&quot;repo&quot;,&quot;path&quot;,&quot;name&quot;)

huangapple
  • 本文由 发表于 2023年6月19日 06:10:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76502715.html
匿名

发表评论

匿名网友

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

确定