exec.Command 使用 JSON 负载转义变量

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

exec.Command Escaping Variables with JSON Payload

问题

非常感谢您的提问。以下是翻译好的内容:

非常感谢您提前的帮助,我已经花了2天的时间在这个问题上。这是一个可用的curl命令。

curl -ku login:pass -X POST -H 'Content-Type: application/json' -d '{"type":"page","title":"Testpage","space":{"key":"ITDept"},"body":{"storage":{"value":"<p>Blank Page.</p>","representation":"storage"}}}' https://confluence/rest/api/content

我需要使用exec.Command来执行这个命令。

在Go语言中,我已经尝试了转义和其他各种方法来使其工作。问题很可能是这个需要的荒谬的JSON字符串。我现在将JSON字符串保存到一个变量中,以尝试这种方式。

jsonPayload := '{"type":"page","title":"Testpage","space":{"key":"ITDept"},"body":{"storage":{"value":"<p>Blank Page.</p>","representation":"storage"}}}'

execCmd := "bash", "-c", "curl -ku login:pass -X POST -H 'Content-Type: application/json' -d" jsonPayload "https://confluence/rest/api/content"

所以jsonPayload是-d的参数。我尝试过使用Marshal json/encoding和net/http包来实现,它可以通过,但是标准库发送它的方式导致API声明它的格式错误。

我还尝试过使用这个方法,将curl命令从println中复制出来,它可以工作,但是在实际运行golang时,它会因为格式不正确而失败。

env := os.Environ()
curlCmd, err := exec.LookPath("curl")
if err != nil {
    fmt.Println("Path not found to binary!")
    panic(err)
}
args := []string{"curl", "-ku", "login:pass", "-X", "POST", "-H", "'Content-Type: application/json'", "-d", payloadJson, "https://confluence/rest/api/content"}

execErr := syscall.Exec(curlcmd, args, env)
if execErr != nil {
    panic(execErr)
}
fmt.Println(curlCmd)

当最后一行的curlCmd打印出来时,可以将其复制粘贴到终端中,它可以工作,但是通过golang运行时,它会显示不支持的格式。非常感谢您的帮助。

英文:

Thank you in advance as I have spent 2 days on this. Here is a working curl command.

curl -ku login:pass -X POST -H &#39;Content-Type: application/json&#39;-d&#39;{&quot;type&quot;:&quot;page&quot;,&quot;title&quot;:&quot;Testpage&quot;,&quot;space&quot;:{&quot;key&quot;:&quot;ITDept&quot;},&quot;body&quot;:{&quot;storage&quot;:{&quot;value&quot;:&quot;&lt;p&gt;Blank Page.&lt;/p&gt;&quot;,&quot;representation&quot;:&quot;storage&quot;}}}&#39; https://confluence/rest/api/content

I need to get this to execute with exec.Command.

Given that now in Go I have tried escaping and all sorts of other means to get this to work. The issue is more than likely this ridiculous JSON string that is required. I have the JSON string saved into a var now to try it that way.

jsonPayload := &#39;{&quot;type&quot;:&quot;page&quot;,&quot;title&quot;:&quot;Testpage&quot;,&quot;space&quot;:{&quot;key&quot;:&quot;ITDept&quot;},&quot;body&quot;:{&quot;storage&quot;:{&quot;value&quot;:&quot;&lt;p&gt;Blank Page.&lt;/p&gt;&quot;,&quot;representation&quot;:&quot;storage&quot;}}}&#39;

execCmd := &quot;bash&quot;, &quot;-c&quot;, &quot;curl -ku login:pass -X POST -H &#39;Content-Type: application/json&#39; -d&quot; jsonPayload &quot;https://confluence/rest/api/content&quot;

So the jsonPayload is the argument to -d. I have tried this using the Marshal json/encoding and the net/http package and it goes through but something about how that stdlib is sending it causes the API to state it is the wrong format.

I also have tried this with this and the curl copied out of the println works but when actually ran in golang it fails with incorrect format.

    env := os.Environ()
	curlCmd, err := exec.LookPath(&quot;curl&quot;)
	if err != nil {
		fmt.Println(&quot;Path not found to binary!&quot;)
		panic(err)
	}
args := []string{&quot;curl&quot;, &quot;-ku&quot;, &quot;login:pass&quot;, &quot;-X&quot;, &quot;POST&quot;, &quot;-H&quot;, &quot;&#39;Content-Type: application/json&#39;&quot;, &quot;-d&quot;, payloadJson, &quot;https://confluence/rest/api/content&quot;}

execErr := syscall.Exec(curlcmd, args, env)
   if execErr != nil {
      panic(execErr)
}
fmt.Println(curlCmd)

When the curlCmd from that last line there prints it can be copied and pasted into the terminal and it works however when going through golang it comes with a format not supported. Any help would be greatly appreciated.

答案1

得分: 1

尝试这个:

payload := `{&quot;type&quot;:&quot;page&quot;,&quot;title&quot;:&quot;Testpage&quot;,&quot;space&quot;:{&quot;key&quot;:&quot;ITDept&quot;},&quot;body&quot;:{&quot;storage&quot;:{&quot;value&quot;:&quot;&lt;p&gt;空白页面。&lt;/p&gt;&quot;,&quot;representation&quot;:&quot;storage&quot;}}}`
cmd := exec.Command(&quot;curl&quot;, &quot;-ku&quot;, &quot;login:pass&quot;, &quot;-X&quot;, &quot;POST&quot;, &quot;-H&quot;, &quot;Content-Type: application/json&quot;, &quot;-d&quot;, payload, &quot;http://localhost:8080/confluence/rest/api/content&quot;)
p, err := cmd.CombinedOutput()
if err != nil {
    log.Fatal(err)
}
fmt.Printf(&quot;%s\n&quot;, p)

与问题中的代码相比的重要更改

- 直接运行命令而不是使用 bash
-  URL 中指定主机名
- 正确引用字符串

顺便说一句你也可以使用解释的字符串字面量

    payload := &quot;{\&quot;type\&quot;:\&quot;page\&quot;,\&quot;title\&quot;:\&quot;Testpage\&quot;,\&quot;space\&quot;:{\&quot;key\&quot;:\&quot;ITDept\&quot;},\&quot;body\&quot;:{\&quot;storage\&quot;:{\&quot;value\&quot;:\&quot;&lt;p&gt;空白页面&lt;/p&gt;\&quot;,\&quot;representation\&quot;:\&quot;storage\&quot;}}}&quot;
英文:

Try this:

payload := `{&quot;type&quot;:&quot;page&quot;,&quot;title&quot;:&quot;Testpage&quot;,&quot;space&quot;:{&quot;key&quot;:&quot;ITDept&quot;},&quot;body&quot;:{&quot;storage&quot;:{&quot;value&quot;:&quot;&lt;p&gt;Blank Page.&lt;/p&gt;&quot;,&quot;representation&quot;:&quot;storage&quot;}}}`
cmd := exec.Command(&quot;curl&quot;, &quot;-ku&quot;, &quot;login:pass&quot;, &quot;-X&quot;, &quot;POST&quot;, &quot;-H&quot;, &quot;Content-Type: application/json&quot;, &quot;-d&quot;, payload, &quot;http://localhost:8080/confluence/rest/api/content&quot;)
p, err := cmd.CombinedOutput()
if err != nil {
log.Fatal(err)
}
fmt.Printf(&quot;%s\n&quot;, p)

Important change from code in the question:

  • Run command directly instead of using bash.
  • Specify host name in URL.
  • Properly quote the string.

BTW, you can also an interpreted string literal:

payload := &quot;{\&quot;type\&quot;:\&quot;page\&quot;,\&quot;title\&quot;:\&quot;Testpage\&quot;,\&quot;space\&quot;:{\&quot;key\&quot;:\&quot;ITDept\&quot;},\&quot;body\&quot;:{\&quot;storage\&quot;:{\&quot;value\&quot;:\&quot;&lt;p&gt;Blank Page.&lt;/p&gt;\&quot;,\&quot;representation\&quot;:\&quot;storage\&quot;}}}&quot;

huangapple
  • 本文由 发表于 2017年1月5日 09:06:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/41475753.html
匿名

发表评论

匿名网友

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

确定