duanluanlang8501 2017-01-05 01:06
浏览 67

带有JSON有效负载的exec.Command转义变量

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条回答 默认 最新

  • dongyun8891 2017-01-05 02:12
    关注

    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
    ", 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\"}}}"
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化