duanbodai5166 2011-11-17 16:17
浏览 460
已采纳

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

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('
');
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()
  • 写回答

2条回答 默认 最新

  • douzhaolu4839 2011-11-17 16:48
    关注

    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)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器