diff options
| author | Shulhan <ms@kilabit.info> | 2024-04-23 19:15:54 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2024-04-24 14:57:02 +0700 |
| commit | 072a5866613aab933d03bef7df5a2dcb3a0855e4 (patch) | |
| tree | 79f185033d4e43fde44ded7bb4e7e59d047e4941 /lib/http/client_example_test.go | |
| parent | 225b6372d0592c2291ff4b9301a68c80d4660d77 (diff) | |
| download | pakakeh.go-072a5866613aab933d03bef7df5a2dcb3a0855e4.tar.xz | |
lib/http: refactoring "multipart/form-data" parameters in ClientRequest
Previously, ClientRequest with type RequestTypeMultipartForm pass the
type "map[string][]byte" in Params.
This type hold the file upload, where key is the file name and []byte is
content of file.
Unfortunately, this model does not correct because a
"multipart/form-data" can contains different field name and file name,
for example
--boundary
Content-Disposition: form-data; name="field0"; filename="file0"
Content-Type: application/octet-stream
<Content of file0>
This changes fix this by changing the parameter type for
RequestTypeMultipartForm to [*multipart.Form], which affect several
functions including [Client.PutFormData] and [GenerateFormData].
We also add new function [CreateMultipartFileHeader] to help creating
[multipart.FileHeader] from raw bytes, that can be assigned to
[*multipart.Form].
Diffstat (limited to 'lib/http/client_example_test.go')
| -rw-r--r-- | lib/http/client_example_test.go | 9 |
1 files changed, 6 insertions, 3 deletions
diff --git a/lib/http/client_example_test.go b/lib/http/client_example_test.go index 7b72cba1..22b24285 100644 --- a/lib/http/client_example_test.go +++ b/lib/http/client_example_test.go @@ -4,6 +4,7 @@ import ( "crypto/rand" "fmt" "log" + "mime/multipart" "strings" libhttp "git.sr.ht/~shulhan/pakakeh.go/lib/http" @@ -15,9 +16,11 @@ func ExampleGenerateFormData() { // NOTE: do not do this on real code. rand.Reader = mock.NewRandReader([]byte(`randomseed`)) - var data = map[string][]byte{ - `name`: []byte(`test.txt`), - `size`: []byte(`42`), + var data = &multipart.Form{ + Value: map[string][]string{ + `name`: []string{`test.txt`}, + `size`: []string{`42`}, + }, } var ( |
