Skip to content

Instantly share code, notes, and snippets.

@ochaloup
Created July 18, 2019 15:19
Show Gist options
  • Select an option

  • Save ochaloup/8405e0008b2775938630f94090bf37b9 to your computer and use it in GitHub Desktop.

Select an option

Save ochaloup/8405e0008b2775938630f94090bf37b9 to your computer and use it in GitHub Desktop.
golang http digest to run WildFly management REST call
/*
* Using https://gowalker.org/github.com/ryanjdew/http-digest-auth-client
* for HTTP digest authentication. The request for the functionality for golang http client
* seems to be tracked at
* https://github.com/golang/go/issues/29409
*/
package main
import (
"fmt"
"net/http"
"strconv"
"time"
"encoding/json"
"bytes"
httpDigestAuth "github.com/ryanjdew/http-digest-auth-client"
)
const (
user = "admin"
pass = "admin"
mgmtPort = 9990
)
func main() {
fmt.Println("Going to probe WildFly transaction store, yay!")
hostName := "localhost" // 127.0.0.1
hostToConnect := "http://" + hostName + ":" + strconv.Itoa(mgmtPort) + "/management"
fmt.Println("Connection to host digest auth", "url", hostToConnect)
httpClient := &http.Client{Timeout: time.Second * 10}
// curl -X POST -uadmin:admin --digest 'http://localhost:9990/management' --header "Content-Type: application/json" -d '{"address": ["subsystem", "transactions", "log-store", "log-store"], "operation":"probe", "json.pretty":1}'
/*
import "strings"
const jsonStream = `{"address": ["subsystem", "transactions", "log-store", "log-store"], "operation":"probe", "json.pretty":1}`
jsonStreamReader := strings.NewReader(jsonStream)
*/
jsonStream := map[string]interface{}{
"address": []string{
"subsystem", "transactions", "log-store", "log-store",
},
"operation": "probe",
}
jsonStreamBytes, err := json.Marshal(jsonStream)
if err != nil {
fmt.Println(err, "Fail to json marshal", "message", jsonStream)
}
jsonStreamReader := bytes.NewBuffer(jsonStreamBytes)
req, err := http.NewRequest("POST", hostToConnect, jsonStreamReader)
if err != nil {
fmt.Println(err, "Fail to create HTTP request", "host name", hostToConnect)
return
}
req.Header.Set("Content-Type", "application/json")
// digestAuth := &httpDigestAuth.DigestHeaders{Username: user, Password: pass}
digestAuth := &httpDigestAuth.DigestHeaders{}
digestAuth, err = digestAuth.Auth(user, pass, hostToConnect)
if err != nil {
fmt.Println(err, "Fail to authenticate", "host name", hostToConnect, "json", jsonStream)
return
}
digestAuth.ApplyAuth(req)
res, err := httpClient.Do(req)
if err != nil {
fmt.Println(err, "Fail invoke http request to management", "host name", hostToConnect, "json", jsonStream)
return
}
if res.StatusCode != http.StatusOK {
fmt.Println("Fail to process operation", "operation", jsonStream, "response", res)
return
}
defer res.Body.Close()
/*
import "io/ioutil"
body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err, "Fail parse body from response", "host name", hostToConnect, "response", res)
return
}
stringBody := string(body)
*/
var jsonBody map[string]interface{}
err = json.NewDecoder(res.Body).Decode(&jsonBody)
if err != nil {
fmt.Println(err, "Fail parse body from response", "host name", hostToConnect, "response", res)
return
}
fmt.Println("<<SUCCESS>> Returned is ", "response", res, "body", jsonBody["outcome"])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment