https://github.com/gowebprod/multipart
https://github.com/gowebprod/multipart
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/gowebprod/multipart
- Owner: GoWebProd
- License: mit
- Created: 2022-01-10T19:37:09.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2022-01-17T11:00:02.000Z (over 3 years ago)
- Last Synced: 2025-01-24T07:42:07.905Z (5 months ago)
- Language: Go
- Size: 7.81 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# multipart
Made on the basis of mime/multipart
## Example
```go
package mainimport (
"context"
"net/http"
"time""github.com/GoWebProd/multipart"
"github.com/pkg/errors"
)func main() {
fileReq, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "http://localhost/testfile", nil)
if err != nil {
panic(errors.Wrap(err, "Can't create new request"))
}httpClient := &http.Client{
Timeout: 5 * time.Second,
}fileResp, err := httpClient.Do(fileReq)
if err != nil {
panic(errors.Wrap(err, "Can't file request"))
}defer fileResp.Body.Close()
writer := multipart.NewWriter()
err = writer.CreateFormFileReader("content", "very_important_file_name", multipart.NewReader(fileResp.Body, int(fileResp.ContentLength)))
if err != nil {
panic(errors.Wrap(err, "Can't create file form"))
}err = writer.CreateFormField("objectType", []byte("file"))
if err != nil {
panic(errors.Wrap(err, "Can't write object type field"))
}
defer writer.Close()req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, "http://localhost/checkfile", writer)
if err != nil {
panic(errors.Wrap(err, "Can't create object scan request"))
}req.ContentLength = int64(writer.Len())
req.Header.Add("Content-Type", writer.FormDataContentType())resp, err := httpClient.Do(req)
if err != nil {
panic(errors.Wrap(err, "Can't do object scan request"))
}defer resp.Body.Close()
}```
## Benchmark
```
goos: darwin
goarch: amd64
pkg: github.com/GoWebProd/multipart
BenchmarkStd-16 465567 2602 ns/op 1345 B/op 37 allocs/op
BenchmarkThis-16 1557962 755 ns/op 275 B/op 7 allocs/op
PASS
ok github.com/GoWebProd/multipart 4.510s
```