Attenzione : necesssita' di GO 1.20. In versioni precedenti viene generato l'errore value.SetZero undefined
La libreria Resty github.com/go-resty/resty/ permette di utilizzare servizi restful anche dietro a proxy e con connessione insecure (connessione https con certificato scaduto)
=========================================================
package main
import (
"crypto/tls"
"fmt"
"os"
"github.com/go-resty/resty/v2" // libreria per rest
)
type AuthSuccess struct {
success string
version string
JWT string
username string
userid string
parentuserid string
userlevelid string
}
func dieOnError(msg string, err error) {
if err != nil {
fmt.Println(msg, err)
os.Exit(1)
}
}
// go get github.com/go-resty/resty/v2
func main() {
connStr := "connessione"
conn, err := go_ora.NewConnection(connStr)
dieOnError("Can't create connection:", err)
err = conn.Open()
dieOnError("Can't open connection:", err)
client := resty.New()
client.SetProxy("http://proxy.xxxxx.toscana.it:8080")
client.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
// User Login
resp, err := client.R().
SetFormData(map[string]string{
"username": "admin",
"password": "xxxxxxx",
"action": "login",
}).
SetResult(&AuthSuccess{}).
Post("https://xxxxxx.xxxxxxx.it/api/")
if err != nil {
fmt.Println("Errore di login")
os.Exit(1)
}
token := resp.Result().(*AuthSuccess).JWT
fmt.Print(token)
}
import (
"crypto/tls"
"fmt"
"os"
"github.com/go-resty/resty/v2" // libreria per rest
)
type AuthSuccess struct {
success string
version string
JWT string
username string
userid string
parentuserid string
userlevelid string
}
func dieOnError(msg string, err error) {
if err != nil {
fmt.Println(msg, err)
os.Exit(1)
}
}
// go get github.com/go-resty/resty/v2
func main() {
connStr := "connessione"
conn, err := go_ora.NewConnection(connStr)
dieOnError("Can't create connection:", err)
err = conn.Open()
dieOnError("Can't open connection:", err)
client := resty.New()
client.SetProxy("http://proxy.xxxxx.toscana.it:8080")
client.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
// User Login
resp, err := client.R().
SetFormData(map[string]string{
"username": "admin",
"password": "xxxxxxx",
"action": "login",
}).
SetResult(&AuthSuccess{}).
Post("https://xxxxxx.xxxxxxx.it/api/")
if err != nil {
fmt.Println("Errore di login")
os.Exit(1)
}
token := resp.Result().(*AuthSuccess).JWT
fmt.Print(token)
}
Nessun commento:
Posta un commento