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 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算