curl -c ./cookies -k -H "Content-Type: application/json" -X POST -d '{"user":"admin","password":"admin"}' "https://hostmane.here.com:443/rest/json/login"
curl -k -s -b ./cookies -H "Content-Type: application/json" -X GET "https://hostmane.here.com:443/rest/json/flows"
package main
import ( "crypto/tls" "errors" "fmt" "io/ioutil" "net/http" "strings" "time")
func main() { authCookie, err := authenticate("hostname", "admin", "admin") if err != nil { fmt.Println("ERROR " + err.Error()) return } fmt.Printf("AUTH SUCCESS:\n%+v\n", authCookie) result, err := queryFlows("hostname", authCookie) if err != nil { fmt.Println("ERROR " + err.Error()) return } fmt.Printf("QUERY SUCCESS:\n%+s\n", result)
}
func authenticate(host, username, password string) (*http.Cookie, error) { tr := &http.Transport{ TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, } client := &http.Client{Transport: tr} body := strings.NewReader(`{"user":"` + username + `","password":"` + password + `"}`) req, err := http.NewRequest("POST", "https://"+host+".here.com:443/rest/json/login", body) if err != nil { return nil, err } req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req) if err != nil { return nil, err } defer resp.Body.Close() respBody, err := ioutil.ReadAll(resp.Body) if err != nil { return nil, err } if resp.StatusCode != 200 { return nil, errors.New(string(respBody)) } cookies := resp.Cookies() authCoookie := cookies[0] return authCoookie, nil}
func queryFlows(host string, authCookie *http.Cookie) (string, error) { tr := &http.Transport{ TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, } client := &http.Client{Transport: tr, Timeout: 1 * time.Minute} req, err := http.NewRequest("Get", "https://"+host+".here.com:443/rest/json/flows", nil) if err != nil { return "", err } req.Header.Set("Content-Type", "application/json") req.AddCookie(authCookie)
resp, err := client.Do(req) if err != nil { return "", err } defer resp.Body.Close() respBody, err := ioutil.ReadAll(resp.Body) if err != nil { return "", err } if resp.StatusCode != 200 { return "", errors.New(string(respBody)) } return string(respBody), nil}
AUTH SUCCESS:
connect.sid=s%3AEaciXI7sKXR3Q1Wv6GU2Yt%2BN.MdlA9oaJuAc1C4MnP%2F997sn1vKvyXyow9bw69fr5ls8; Path=/; HttpOnly
ERROR <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>502 Proxy Error</title>
</head><body>
<h1>Proxy Error</h1>
<p>The proxy server received an invalid
response from an upstream server.<br />
The proxy server could not handle the request <em><a href="/rest/json/flows">Get /rest/json/flows</a></em>.<p>
Reason: <strong>Error reading from remote server</strong></p></p>
</body></html>
Get /rest/json/flows HTTP/1.1
Host: host.here.com:443
User-Agent: Go-http-client/1.1
Content-Type: application/json
Cookie: connect.sid=s%3A%2FWuxV7HdAnHDweXFvs4N8%2BaB.DuNDxfVN6sLhLO%2Flu3hIY7PfUnfMquyRIfXSllGtZpM
Accept-Encoding: gzip
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.