forked from fwtpe/owl-backend
/
common.go
43 lines (37 loc) · 958 Bytes
/
common.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package PortalTest
import (
"fmt"
"io/ioutil"
"net/http"
"strings"
"github.com/Jeffail/gabs"
"github.com/parnurzeal/gorequest"
)
var host = "127.0.0.1:1234"
var name = "root"
var password = "password"
var session = ""
var request = gorequest.New()
type Resps struct {
Body string
Code int
Error error
}
func DoPost(url string, params string) (myresp Resps) {
postdata := strings.NewReader(params)
posturl := fmt.Sprintf("http://%s%s", host, url)
resp, err := http.Post(posturl, "application/x-www-form-urlencoded", postdata)
if err == nil {
body, _ := ioutil.ReadAll(resp.Body)
myresp.Body = string(body)
}
myresp.Code = resp.StatusCode
myresp.Error = err
return
}
func GetAuthSessoion() (string, string) {
resp := DoPost("/api/v1/auth/login", fmt.Sprintf(`name=%s;password=%s`, name, password))
jsParsed, _ := gabs.ParseJSON([]byte(resp.Body))
session = jsParsed.Search("data", "sig").Data().(string)
return name, session
}