curl command in exec.Command does not return same data as cli

1,309 views
Skip to first unread message

Sathish VJ

unread,
Jan 3, 2016, 12:52:27 PM1/3/16
to golang-nuts
I'm attempting to run a unix command to retrieve the url that a short url evaluates to.  It works if I run curl from the command line.  But running it from within Go gives me a 400 Bad Request.  

What would I need to understand to figure out the difference in behavior?

Also, is there another way of figuring out the effective url in go?


package main

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

func main() {
cmdStr := fmt.Sprintf("%s -s -L -o /dev/null -w '%%{url_effective}'", shortUrl)
fmt.Println("Cmd is: ", cmdStr)

out, err := exec.Command("curl", cmdStr).Output()
if err != nil {
fmt.Println("Error!")
log.Fatal(err)
}
fmt.Println("Received output for shortUrl ", shortUrl, ":", string(out))

}





Jakob Borg

unread,
Jan 3, 2016, 1:51:22 PM1/3/16
to Sathish VJ, golang-nuts
2016-01-03 18:52 GMT+01:00 Sathish VJ <sath...@gmail.com>:
> I'm attempting to run a unix command to retrieve the url that a short url
> evaluates to. It works if I run curl from the command line. But running it
> from within Go gives me a 400 Bad Request.

> cmdStr := fmt.Sprintf("%s -s -L -o /dev/null -w '%%{url_effective}'", shortUrl)
> out, err := exec.Command("curl", cmdStr).Output()

This needs to be split into something like

out, err := exec.Command("curl", shortUrl, "-s", "-L", ...) // etc

as you're not passing through a shell that does argument splitting.
But really, there's no reason to use curl from within Go, unless
you're just doing this to experiment with running external commands.

//jb

Donovan

unread,
Jan 3, 2016, 8:24:53 PM1/3/16
to golang-nuts
All you need is to do a HEAD request against it from go: 

https://play.golang.org/p/0DOM_VNadH

won't work on playground (don't allow http / https request), but works locally

Norbert Roos

unread,
Jan 3, 2016, 8:25:15 PM1/3/16
to golan...@googlegroups.com
On 03.01.2016 18:52, Sathish VJ wrote:

> It works if I run curl from the command line. But running
> it from within Go gives me a 400 Bad Request.

I guess you are not running the command on the commandline like this,
which should give you an error:

$ curl "https://youtu.be/_zqppDTWV64 -s -L -o /dev/null -w
'%%{url_effective}'"

But this is what are you are doing from your program.

You need to create an array/slice with each program parameter as a
separate string and pass this array to exec.Command().


Reply all
Reply to author
Forward
0 new messages