utils.go 525 B

1234567891011121314151617181920212223242526
  1. package multipartestutils
  2. import (
  3. "bytes"
  4. "mime"
  5. "mime/multipart"
  6. )
  7. func CreateMultipartFileHeader(filename string, body []byte) *multipart.FileHeader {
  8. contentType, rd := NewMultipartBuilder().
  9. AddReader("test", filename, bytes.NewBuffer(body)).
  10. Build()
  11. defer rd.Close()
  12. _, params, err := mime.ParseMediaType(contentType)
  13. if err != nil {
  14. panic(err)
  15. }
  16. mr := multipart.NewReader(rd, params["boundary"])
  17. form, err := mr.ReadForm(1024 * 1024)
  18. if err != nil {
  19. panic(err)
  20. }
  21. return form.File["test"][0]
  22. }