3

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()
2
  • What do you mean with "it isn't working"? Do you get an error? If so, which? Commented Nov 17, 2011 at 16:20
  • Nope, I dont get an error, but I think there something wrong with the curl command :/ Commented Nov 17, 2011 at 16:42

2 Answers 2

6

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)
}
Sign up to request clarification or add additional context in comments.

2 Comments

When I add the second code I always get in output, even if the dent was published succsesfull...
What you could do instead of connecting c.Stdout to os.Stdout is use the Output() method of Cmd which is similar, but return the output of the command in a byte slice.
1

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.

1 Comment

strings.Split(arguments, " ") won't actually work all the time with curl. Since you can have spaces within your header values, etc it will break

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.