为什么这个curl命令不起作用?

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

Why is this curl command not working?

问题

嗨,我只想创建一个简单的golang应用程序,它使用以下命令在identi.ca上发布一个新的dent:

curl -u username:password http://example.com/api/statuses/update.xml -d status='Howdy!' -d lat='30.468' -d long='-94.743'

这是我目前的代码,我认为它应该可以工作,但实际上它并没有工作,有人知道如何修复吗?

编辑: 不,我没有收到任何错误消息 :/

package main

import (
    "fmt"
    "os"
    "bufio"
    "os/exec"
)

func main() {

    var err error
    var username string

    fmt.Print("Username: ")
    _, err = fmt.Scanln(&username)
    if err != nil {
        fmt.Println("Error: ", err)
    }

    var password string
    fmt.Print("Password: ")
    _, err = fmt.Scanln(&password)
    if err != nil {
        fmt.Println("Error: ", err)
    }

    var status string
    fmt.Print("Status: ")
    in := bufio.NewReader(os.Stdin)
    status, err = in.ReadString('\n')
    if err != nil {
        fmt.Println("Error: ", err)
    }

    exec.Command("curl -u " + username + ":" + password + "https://identi.ca/api/statuses/update.xml -d status='" + status + "' -d source='API'").Run()
}
英文:

Hi there I just want to create a simple golang applications, which posts a new dent at identi.ca using

curl -u username:password http://example.com/api/statuses/update.xml -d status='Howdy!' -d lat='30.468' -d long='-94.743'

This is my code so far and imho this should work, but actually it isn't working, does anybody know how to fix this?

EDIT: Nope: I don't get any error messages :/

package main

import(
        "fmt"
		"os"
		"bufio"
		"exec"
)
func main() {

var err os.Error
var username string

print("Username: ")
_, err = fmt.Scanln(&username)
if err != nil {
    fmt.Println("Error: ", err)
}

var password string
print("Password: ")
_, err = fmt.Scanln(&password)
if err != nil {
    fmt.Println("Error: ", err)
}

var status string
print("Status: ")
in := bufio.NewReader(os.Stdin);
status, err = in.ReadString('\n');
if err != nil {
    fmt.Println("Error: ", err)
}

exec.Command("curl -u " + username + ":" + password + "https://identi.ca/api/statuses/update.xml -d status='" + status +  "'" + "-d source='API'").Run()

答案1

得分: 6

exec.Command()不会将整个命令行作为单个参数传递。你需要这样调用它:

exec.Command("curl", "-u", username+":"+password, ...url..., "-d", "status="+status, "-d", "source=API").Run()

你如何知道是否出现错误?你没有检查Run()的返回值。

实际上,你应该将命令的创建与运行分开。这样你可以将进程的stdout和stderr设置为除了/dev/null之外的其他内容,例如:

c := exec.Command("curl", "-u", username+":"+password, "https://identi.ca/api/statuses/update.xml", "-d", "status="+status, "-d", "source=API")
c.Stdout = os.Stdout
c.Stderr = os.Stderr
err = c.Run()
if err != nil {
    fmt.Println("Error: ", err)
}
英文:

exec.Command() doesn't take the whole command line as a single argument. You need to call it as:

exec.Command("curl", "-u", username+":"+password, ...url..., "-d", "status="+status, "-d", "source=API").Run()

How do you know if you get an error? You don't check the return value of Run().

You should actually separate the command creation from running it. This way you can set the process's stdout and stderr to something besides /dev/null, e.g.

c := exec.Command("curl", "-u", username+":"+password, "https://identi.ca/api/statuses/update.xml", "-d", "status="+status, "-d", "source=API")
c.Stdout = os.Stdout
c.Stderr = os.Stderr
err = c.Run()
if err != nil {
	fmt.Println("Error: ", err)
}

答案2

得分: 1

这里所解释的那样,exec.Command不会像普通的shell环境(如bash、cmd等)那样拆分命令行参数。

因此,除了填写参数如下:

exec.Command("your_executable", "-switch1", "value1", "-switch2", "value2")

你可以让bash或cmd(Windows)帮助你:

exec.Command("cmd", "/C", "your_executable -switch1 value1 -switch2 value2")

或者你可以自己拆分参数字符串(参考):

arguments := "-switch1 value1 -switch2 value2"

arg_slice := strings.Split(arguments, " ")

exec.Command("your_executable", arg_slice...)

这是最方便的,因为你可以在运行时构建arguments字符串。

英文:

As explained here, exec.Command doesn't split the command line arguments like your normal shell environment does (bash, cmd, etc).

So in addition to filling in the parameters as

exec.Command("your_executable", "-switch1", "value1", "-switch2", "value2")

you can have bash or cmd (Windows) help you

exec.Command("cmd","/C", "your_executable -switch1 value1 -switch2 value2")

or you can split the argument string yourself (ref)

arguments := "-switch1 value1 -switch2 value2"

arg_slice := strings.Split(arguments, " ")

exec.Command("your_executable", arg_slice...)

This is most convenient, since you may build up the arguments string at run-time.

huangapple
  • 本文由 发表于 2011年11月18日 00:17:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/8170529.html
匿名

发表评论

匿名网友

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

确定