本文整理匯總了Golang中github.com/franela/goreq.Request類的典型用法代碼示例。如果您正苦於以下問題:Golang Request類的具體用法?Golang Request怎麽用?Golang Request使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Request類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: LogRequestFromValidationResult
//LogRequestFromValidationResult unmarshalls the ValidationResult and logs to keen.io
//
//http://api.keen.io/3.0/projects/<project_id>/events/<event_collection>
func (keen *KeenMetrics) LogRequestFromValidationResult(collectionName string, validationResult string) {
var url = keen.getEndpoint() + collectionName
var result goiban.ValidationResult
json.Unmarshal([]byte(validationResult), &result)
req := goreq.Request{
Method: "POST",
Uri: url,
ContentType: "application/json",
Body: ValidationResultToEvent(&result),
}
req.AddHeader("Authorization", keen.WriteAPIKey)
res, err := req.Do()
if err != nil {
log.Printf("Error while posting stats: %v", err)
return
}
// Close the response body
if res.Body != nil {
defer res.Body.Close()
}
if collectionName == "Test" {
log.Printf(url)
text, _ := res.Body.ToString()
log.Printf("Response (%v): %v", res.StatusCode, text)
}
}
示例2: Worker
func (m webhooksAlerter) Worker(q chan webhook) {
for {
select {
case webhook := <-q:
log.Info("Sending webhook alert to %s", webhook.Url)
req := goreq.Request{
Uri: webhook.Url,
Accept: "application/json",
ContentType: "application/json",
UserAgent: "Lovebeat",
Timeout: 10 * time.Second,
Body: webhook.Data,
}
req.AddHeader("X-Lovebeat", "1")
_, err := req.Do()
if err != nil {
log.Error("Failed to post webhook: %s", err)
}
}
}
}
示例3: post
func (c Client) post(url string, requestData interface{}) []error {
var errs []error
req := goreq.Request{
Method: "POST",
Body: requestData,
Uri: url,
ContentType: "application/x-www-form-urlencoded; charset=UTF-8",
CookieJar: c.cookie,
}
req.AddHeader("X-Requested-With", "XMLHttpRequest")
resp, err := req.Do()
if err != nil {
errs = append(errs, errors.New(err.Error()))
log.Fatalln(err.Error())
}
defer func() {
err := resp.Body.Close()
if err != nil {
log.Fatal(err)
}
}()
if resp.StatusCode != http.StatusOK {
errs = append(errs, errors.New("Failed login: status code is "+resp.Status))
}
return errs
}
示例4: sendRequest
// sendRequest is an internal method to send the prepared requests to OpsGenie.
func (cli *OpsGenieClient) sendRequest(req goreq.Request) (*goreq.Response, error) {
// send the request
var resp *goreq.Response
var err error
for i := 0; i < cli.httpTransportSettings.MaxRetryAttempts; i++ {
resp, err = req.Do()
if err == nil && resp.StatusCode < 500 {
break
}
if resp != nil {
defer resp.Body.Close()
logging.Logger().Info(fmt.Sprintf("Retrying request [%s] ResponseCode:[%d]. RetryCount: %d", req.Uri, resp.StatusCode, (i + 1)))
} else {
logging.Logger().Info(fmt.Sprintf("Retrying request [%s] Reason:[%s]. RetryCount: %d", req.Uri, err.Error(), (i + 1)))
}
time.Sleep(timeSleepBetweenRequests * time.Duration(i+1))
}
if err != nil {
message := "Unable to send the request " + err.Error()
logging.Logger().Warn(message)
return nil, errors.New(message)
}
// check for the returning http status
statusCode := resp.StatusCode
if statusCode >= 400 {
body, err := resp.Body.ToString()
if err != nil {
message := "Server response with error can not be parsed " + err.Error()
logging.Logger().Warn(message)
return nil, errors.New(message)
}
return nil, errorMessage(statusCode, body)
}
return resp, nil
}
示例5: callURL
func (wa *webAuth) callURL(url, method string, body interface{}, statusCode int, resJSON interface{}) (res *goreq.Response, err error) {
req := goreq.Request{
Method: method,
Uri: url,
Body: body,
CookieJar: wa.jar,
Accept: "application/json",
Host: "hub.docker.com",
ContentType: "application/json",
}.WithHeader("Referer", "https://hub.docker.com/login/")
if wa.token != "" {
req = req.WithHeader("Authorization", fmt.Sprintf("JWT %v", wa.token))
}
res, err = req.Do()
if err != nil {
return res, wrapError(err, fmt.Sprintf("%v to %v", method, url))
}
if statusCode != 0 && res.StatusCode != statusCode {
return res, wrongResponseError(res,
fmt.Sprintf("%v to %v should have returned a %v", method, url, statusCode))
}
if resJSON == nil {
return
}
err = res.Body.FromJsonTo(resJSON)
if err != nil {
return res, wrapError(err, fmt.Sprintf("extracting JSON from %v to %v", method, url))
}
return
}
示例6: GetPreAuthCode
// 獲取預授權碼,用於公眾號oauth
func (ra *regularApi) GetPreAuthCode(accessToken string) (string, float64) {
postData := struct {
Component_appid string `json:"component_appid"`
}{
Component_appid: ra.wt.appId,
}
res, err := goreq.Request{
Method: "POST",
Uri: fmt.Sprintf(apiCreatePreAuthCode, accessToken),
Body: postData,
}.Do()
if err != nil {
log.Println("getPreAuthCode api failed: ", err.Error())
return "", 0
}
result := &struct {
PAC string `json:"pre_auth_code"`
ExpiresIn float64 `json:"expires_in"`
}{}
err = unmarshalResponseToJson(res, result)
if err != nil {
log.Println("Parse pre auth token failed: ", err)
return "", 0
}
return result.PAC, result.ExpiresIn
}
示例7: GetAccessToken
// 獲取第三方平台令牌
func (ra *regularApi) GetAccessToken(ticket string) (string, float64) {
postData := struct {
Component_appid string `json:"component_appid"`
Component_appsecret string `json:"component_appsecret"`
Component_verify_ticket string `json:"component_verify_ticket"`
}{
Component_appid: ra.wt.appId,
Component_appsecret: ra.wt.appSecret,
Component_verify_ticket: ticket,
}
res, err := goreq.Request{
Method: "POST",
Uri: apiComponentToken,
Body: postData,
}.Do()
if err != nil {
log.Println("getAccessToken api failed: ", err.Error())
return "", 0
}
result := &struct {
CAT string `json:"component_access_token"`
ExpiresIn float64 `json:"expires_in"`
}{}
err = unmarshalResponseToJson(res, result)
if err != nil {
log.Println("Parse access token failed: ", err)
}
return result.CAT, result.ExpiresIn
}
示例8: getObject
//retrieve object from the object store
func (u *UserGraphResource) getObject(id string, response *restful.Response) (*Object, error) {
res, err := goreq.Request{
Uri: u.baseUrl + id,
Accept: "application/json",
ContentType: "application/json",
}.Do()
if err != nil {
response.AddHeader("Content-Type", "text/plain")
response.WriteErrorString(http.StatusInternalServerError, err.Error())
return nil, errors.New("500")
}
if res.StatusCode == 404 {
response.AddHeader("Content-Type", "text/plain")
response.WriteErrorString(http.StatusNotFound, "Requested item could not be found.")
return nil, errors.New("404")
}
if res.StatusCode != 200 {
response.AddHeader("Content-Type", "text/plain")
value, _ := res.Body.ToString()
response.WriteErrorString(http.StatusInternalServerError, "Object store returned: "+
strconv.Itoa(res.StatusCode)+" Response body:\n"+value)
return nil, errors.New(strconv.Itoa(res.StatusCode))
}
obj := new(Object)
res.Body.FromJsonTo(&obj)
res.Body.Close()
return obj, nil
}
示例9: WriteLogRequest
//WriteLogRequest logs to keen.io
//
// http://api.keen.io/3.0/projects/<project_id>/events/<event_collection>
func (keen *KeenMetrics) WriteLogRequest(collectionName string, iban *goiban.Iban) {
var url = keen.getEndpoint() + collectionName
req := goreq.Request{
Method: "POST",
Uri: url,
ContentType: "application/json",
Body: IbanToEvent(iban),
}
req.AddHeader("Authorization", keen.WriteAPIKey)
res, err := req.Do()
if err != nil {
log.Printf("Error while posting stats: %v", err)
return
}
// Close the response body
if res.Body != nil {
defer res.Body.Close()
}
if collectionName == "Test" {
log.Printf(url)
text, _ := res.Body.ToString()
log.Printf("Response (%v): %v", res.StatusCode, text)
}
}
示例10: Dispatch
func Dispatch(req goreq.Request) (string, error) {
// --debug to show only request uri (plus data if POSTing)
for _, arg := range os.Args {
if arg == "-d" || arg == "--debug" {
httpreq, _ := req.NewRequest()
fmt.Printf("%# v", pretty.Formatter(httpreq))
os.Exit(0)
}
}
// dispatch
res, err := req.Do()
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
body, err := res.Body.ToString()
// --prettify
for _, arg := range os.Args {
if arg == "--prettify" {
body, _ = PrettifyJson(body)
}
}
return body, err
}
示例11: Worker
func (m slackhookAlerter) Worker(q chan slackhook, cfg *config.ConfigSlackhook) {
for {
select {
case slackhook := <-q:
var err error
var context = make(map[string]interface{})
context["View"] = slackhook.Data.View
context["Previous"] = slackhook.Data.Previous
context["Current"] = slackhook.Data.Current
var doc bytes.Buffer
err = m.template.Execute(&doc, context)
if err != nil {
log.Error("Failed to render template", err)
return
}
req := goreq.Request{
Method: "POST",
Uri: cfg.Uri,
Accept: "*/*",
ContentType: "application/x-www-form-urlencoded",
UserAgent: "Lovebeat",
Timeout: 10 * time.Second,
Body: "payload=" + url.QueryEscape(doc.String()),
}
req.AddHeader("X-Lovebeat", "1")
res, err := req.Do()
if err != nil {
log.Error("Failed to post slackhook:%v:", err)
}
robots, err := ioutil.ReadAll(res.Body)
res.Body.Close()
//it returned a 200 so ignore any error here
if err != nil {
log.Error("OK:unreadable response:%v:", err)
} else if res.StatusCode != http.StatusOK {
log.Error("NOK:non-200:%d:", res.StatusCode)
} else {
log.Info("OK:response:%s:", string(robots))
}
}
}
}
示例12: get
func (c Client) get(url string, requestData interface{}, result interface{}) []error {
var errs []error
resp, err := goreq.Request{
Uri: url,
QueryString: requestData,
CookieJar: c.cookie,
}.Do()
if err != nil {
errs = append(errs, errors.New(err.Error()))
log.Fatalln(err.Error())
}
defer func() {
err := resp.Body.Close()
if err != nil {
log.Fatal(err)
}
}()
if resp.StatusCode != http.StatusOK {
errs = append(errs, errors.New("Failed login: status code is "+resp.Status))
}
err = resp.Body.FromJsonTo(&result)
if err != nil {
errs = append(errs, errors.New(err.Error()))
log.Println(err.Error())
}
return errs
}
示例13: serveValidateServerAuth
func serveValidateServerAuth(backend backend, w http.ResponseWriter, req *http.Request) bool {
info := backend.GetInfo()
serverAuth := info.ServerAuth
if serverAuth == nil {
return false
}
if !strings.HasSuffix(req.URL.Path, SERVER_AUTH_ENDPOINT) {
return false
}
originalPath := strings.Replace(req.URL.Path, SERVER_AUTH_ENDPOINT, "", 1)
if code := req.URL.Query().Get("code"); code != "" {
fmt.Printf("Asking server %s about code %s\n", serverAuth.ValidateUrl, code)
gr := goreq.Request{
Method: "POST",
Uri: serverAuth.ValidateUrl,
ContentType: "application/x-www-form-urlencoded",
Accept: "application/json",
UserAgent: "Undergang/1.0",
Body: "code=" + code + "&host=" + req.Host + "&path=" + originalPath,
Timeout: 5 * time.Second,
}
var parsed struct {
// Not really used.
AccessToken string `json:"access_token"`
}
if ret, err := gr.Do(); err == nil && ret.StatusCode == 200 {
if ret.Body.FromJsonTo(&parsed) == nil && parsed.AccessToken != "" {
cookie := &http.Cookie{
Path: info.Prefix,
Name: SERVER_AUTH_COOKIE,
Value: NewTimestampSigner(sha1.New()).Sign(getCookieToken(info)),
}
http.SetCookie(w, cookie)
fmt.Println("User authenticated!")
http.Redirect(w, req, originalPath, 302)
} else {
respond(w, req, "Authentication server failure", http.StatusForbidden)
}
} else {
respond(w, req, "Authentication server denied code", http.StatusForbidden)
}
} else {
respond(w, req, "No code provided", http.StatusForbidden)
}
return true
}
示例14: AuthorizeRequest
func AuthorizeRequest(path string, w http.ResponseWriter, r *http.Request, config Config) (result AuthResponse, err error) {
if config.AuthURL == "" {
result = AuthResponse{path, false, false}
return
}
username, password, ok := basicAuth(r)
if !ok {
err = errors.New("No HTTP credentials were supplied. Issuing challenge.")
writeChallenge(w)
return
}
reqBody, err := json.Marshal(map[string]string{"username": username, "password": password, "path": path})
if err != nil {
log.Println(err)
return
}
req := goreq.Request{
Method: "POST",
Uri: config.AuthURL,
ContentType: "application/json",
Accept: "application/json",
Body: reqBody,
Timeout: 3000 * time.Millisecond,
}
res, err := req.Do()
if err != nil {
return
}
switch res.StatusCode {
case 200, 201:
res.Body.FromJsonTo(&result)
case 204:
result = AuthResponse{path, false, false}
case 401, 403:
body, er := res.Body.ToString()
if er != nil {
body = "Unauthorized"
}
err = AuthError{res.StatusCode, body}
case 404:
err = AuthError{res.StatusCode, "The repository could not be found"}
default:
err = AuthError{res.StatusCode, "An unknown error occurred"}
}
return
}
示例15: buildCommonRequestProps
// buildCommonRequestProps is an internal method to set common properties of requests that will send to OpsGenie.
func (cli *OpsGenieClient) buildCommonRequestProps() goreq.Request {
if cli.httpTransportSettings == nil {
cli.makeHTTPTransportSettings()
}
goreq.SetConnectTimeout(cli.httpTransportSettings.ConnectionTimeout)
req := goreq.Request{}
if cli.proxy != nil {
req.Proxy = cli.proxy.toString()
}
req.UserAgent = userAgentParam.ToString()
req.Timeout = cli.httpTransportSettings.RequestTimeout
req.Insecure = true
return req
}