38 lines
803 B
Go
38 lines
803 B
Go
package q5
|
|
|
|
import (
|
|
"net/http"
|
|
"io/ioutil"
|
|
"errors"
|
|
)
|
|
|
|
func HttpGet(url string, params *XObject) (string, error) {
|
|
if !StrContains(url, "?") {
|
|
url = url + "?"
|
|
}
|
|
if resp, err := http.Get(url); err == nil {
|
|
if resp.StatusCode != 200 {
|
|
return "", errors.New("HttpGet error Status:" + resp.Status)
|
|
}
|
|
if bytes, err := ioutil.ReadAll(resp.Body); err == nil {
|
|
return string(bytes), nil
|
|
} else {
|
|
return "", err
|
|
}
|
|
} else {
|
|
return "", err
|
|
}
|
|
}
|
|
|
|
func HttpGetAsJson(url string, params *XObject) (*XObject, string, error) {
|
|
respStr, err := HttpGet(url, params)
|
|
if err != nil {
|
|
return nil, respStr, err
|
|
}
|
|
respObj := NewXoFromJsonStr(respStr)
|
|
if !respObj.IsObject() {
|
|
return nil, respStr, errors.New("HttpGetAsJson error invalid json format")
|
|
}
|
|
return respObj, respStr, err
|
|
}
|