aboutsummaryrefslogtreecommitdiff
path: root/src/mime/multipart/example_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/mime/multipart/example_test.go')
-rw-r--r--src/mime/multipart/example_test.go40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/mime/multipart/example_test.go b/src/mime/multipart/example_test.go
index fe154ac4f6..7a22ff789e 100644
--- a/src/mime/multipart/example_test.go
+++ b/src/mime/multipart/example_test.go
@@ -50,3 +50,43 @@ func ExampleNewReader() {
// Part "one": "A section"
// Part "two": "And another"
}
+
+func ExampleReader_ReadForm() {
+ body := strings.NewReader(
+ "--foo\r\nContent-Disposition: form-data; name=\"field1\"\r\n\r\n" +
+ "Value of field.\r\n" +
+ "--foo\r\nContent-Disposition: form-data; name=\"file1\"; filename=\"file1.txt\"\r\n\r\n" +
+ "Value of file1.\r\n" +
+ "--foo--\r\n")
+
+ mr := multipart.NewReader(body, "foo")
+ form, err := mr.ReadForm(0)
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ for name, val := range form.Value {
+ fmt.Printf("Form.Value %q: %q\n", name, val)
+ }
+ for name, listHeader := range form.File {
+ for _, hdr := range listHeader {
+ file, err := hdr.Open()
+ if err != nil {
+ log.Fatal(err)
+ }
+ content, err := io.ReadAll(file)
+ if err != nil {
+ log.Fatal(err)
+ }
+ fmt.Printf("Form.File %q:\n", name)
+ fmt.Printf(" Filename: %q\n", hdr.Filename)
+ fmt.Printf(" Content: %q\n", content)
+ }
+ }
+
+ // Output:
+ // Form.Value "field1": ["Value of field."]
+ // Form.File "file1":
+ // Filename: "file1.txt"
+ // Content: "Value of file1."
+}