英文:
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 '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
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 := '{"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"
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("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)
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 := `{"type":"page","title":"Testpage","space":{"key":"ITDept"},"body":{"storage":{"value":"<p>空白页面。</p>","representation":"storage"}}}`
cmd := exec.Command("curl", "-ku", "login:pass", "-X", "POST", "-H", "Content-Type: application/json", "-d", payload, "http://localhost:8080/confluence/rest/api/content")
p, err := cmd.CombinedOutput()
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", p)
与问题中的代码相比的重要更改:
- 直接运行命令,而不是使用 bash。
- 在 URL 中指定主机名。
- 正确引用字符串。
顺便说一句,你也可以使用解释的字符串字面量:
payload := "{\"type\":\"page\",\"title\":\"Testpage\",\"space\":{\"key\":\"ITDept\"},\"body\":{\"storage\":{\"value\":\"<p>空白页面。</p>\",\"representation\":\"storage\"}}}"
英文:
Try this:
payload := `{"type":"page","title":"Testpage","space":{"key":"ITDept"},"body":{"storage":{"value":"<p>Blank Page.</p>","representation":"storage"}}}`
cmd := exec.Command("curl", "-ku", "login:pass", "-X", "POST", "-H", "Content-Type: application/json", "-d", payload, "http://localhost:8080/confluence/rest/api/content")
p, err := cmd.CombinedOutput()
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", 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 := "{\"type\":\"page\",\"title\":\"Testpage\",\"space\":{\"key\":\"ITDept\"},\"body\":{\"storage\":{\"value\":\"<p>Blank Page.</p>\",\"representation\":\"storage\"}}}"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论