为什么这个curl命令不起作用? [英] Why is this curl command not working?
问题描述
您好,我只想创建一个简单的golang应用程序,它使用在identi.ca上发布一个新的凹痕.
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?
:不,我没有收到任何错误消息:/
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()
推荐答案
exec.Command()
并不将整个命令行作为单个参数.您需要将其称为:
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()
您怎么知道是否遇到错误?您无需检查 Run()
的返回值.
How do you know if you get an error? You don't check the return value of Run()
.
您实际上应该将命令创建与运行分开.这样,您可以将进程的stdout和stderr设置为/dev/null
之外的其他值,例如
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)
}
这篇关于为什么这个curl命令不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!