You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
2.9KB

  1. package main
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "io/ioutil"
  7. "log"
  8. "mime/multipart"
  9. "net/http"
  10. "net/http/httputil"
  11. "os"
  12. )
  13. //post a file to a url
  14. func postFileForm(filename string, targetURL string, formFieldName string) (strJSON string, err error) {
  15. bodyBuf := &bytes.Buffer{}
  16. bodyWriter := multipart.NewWriter(bodyBuf)
  17. // this step is very important
  18. fileWriter, err := bodyWriter.CreateFormFile(formFieldName, filename)
  19. if err != nil {
  20. fmt.Println(err)
  21. return "", err
  22. }
  23. // open file handle
  24. fh, err := os.Open(filename)
  25. if err != nil {
  26. fmt.Println(err)
  27. return "", err
  28. }
  29. //iocopy
  30. _, err = io.Copy(fileWriter, fh)
  31. if err != nil {
  32. return "", err
  33. }
  34. contentType := bodyWriter.FormDataContentType()
  35. bodyWriter.Close()
  36. resp, err := http.Post(targetURL, contentType, bodyBuf)
  37. if err != nil {
  38. return "", err
  39. }
  40. defer resp.Body.Close()
  41. r, err := ioutil.ReadAll(resp.Body)
  42. strJSON = string(r)
  43. if err != nil {
  44. return "", err
  45. }
  46. fmt.Println(resp.Status)
  47. fmt.Println(string(strJSON))
  48. return strJSON, nil
  49. }
  50. func postJSON(jsonB []byte, targetURL string) (resp string, err error) {
  51. headers := map[string]string{}
  52. headers["Content-Type"] = "application/json"
  53. return postRAW(jsonB, targetURL, headers)
  54. }
  55. func postRAW(data []byte, targetURL string, headers map[string]string) (resp string, err error) {
  56. return httpRaw("POST", targetURL, data, headers)
  57. }
  58. func putRAW(data []byte, targetURL string, headers map[string]string) (resp string, err error) {
  59. return httpRaw("PUT", targetURL, data, headers)
  60. }
  61. func patchRAW(data []byte, targetURL string, headers map[string]string) (resp string, err error) {
  62. return httpRaw("PATCH", targetURL, data, headers)
  63. }
  64. func getRAW(targetURL string, headers map[string]string) (resp string, err error) {
  65. return httpRaw("GET", targetURL, nil, headers)
  66. }
  67. func deleteRAW(targetURL string, headers map[string]string) (resp string, err error) {
  68. return httpRaw("DELETE", targetURL, nil, headers)
  69. }
  70. func httpRaw(httpMethod, targetURL string, data []byte, headers map[string]string) (resp string, err error) {
  71. requestBody := io.Reader(nil)
  72. if data != nil {
  73. requestBody = bytes.NewBuffer(data)
  74. }
  75. req, err := http.NewRequest(httpMethod, targetURL, requestBody)
  76. if err != nil {
  77. return
  78. }
  79. if headers != nil {
  80. for k, v := range headers {
  81. req.Header.Set(k, v)
  82. }
  83. }
  84. client := &http.Client{}
  85. r, err := client.Do(req)
  86. if err != nil {
  87. return
  88. }
  89. defer r.Body.Close()
  90. b, _ := ioutil.ReadAll(r.Body)
  91. resp = string(b)
  92. if r.StatusCode != 200 {
  93. debugDumpHTTPResponse(r)
  94. err = errorHTTPResponseNew(r, r.Header.Get("X-Status-Reason"))
  95. }
  96. //log.Println(resp)
  97. return
  98. }
  99. func debugDumpHTTPResponse(r *http.Response) {
  100. dump, e := httputil.DumpResponse(r, true)
  101. if e != nil {
  102. log.Fatal(e)
  103. }
  104. fmt.Printf("dump http Response \n : %s", dump)
  105. }
  106. func debugDumpHTTPRequest(r *http.Request) {
  107. dump, err := httputil.DumpRequest(r, true)
  108. if err != nil {
  109. log.Fatal(err)
  110. }
  111. fmt.Printf("dump http request : %q", dump)
  112. }