auth_request.go 880 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package requests
  2. import (
  3. "github.com/toolkits/net/httplib"
  4. "time"
  5. "encoding/json"
  6. "errors"
  7. )
  8. func CurlPlus(uri, method, token_name, token_sig string, headers, params map[string]string) (req *httplib.BeegoHttpRequest, err error) {
  9. if method == "GET" {
  10. req = httplib.Get(uri)
  11. } else if method == "POST" {
  12. req = httplib.Post(uri)
  13. } else if method == "PUT" {
  14. req = httplib.Put(uri)
  15. } else if method == "DELETE" {
  16. req = httplib.Delete(uri)
  17. } else if method == "HEAD" {
  18. req = httplib.Head(uri)
  19. } else {
  20. err = errors.New("invalid http method")
  21. return
  22. }
  23. req = req.SetTimeout(1*time.Second, 5*time.Second)
  24. token, _ := json.Marshal(map[string]string{
  25. "name": token_name,
  26. "sig": token_sig,
  27. })
  28. req.Header("Apitoken", string(token))
  29. for hk, hv := range headers {
  30. req.Header(hk, hv)
  31. }
  32. for pk, pv := range params {
  33. req.Param(pk, pv)
  34. }
  35. return
  36. }