Documentation ¶
Overview ¶
Package helpers helps writing file parts to MIME multipart messages while preserving the original content type inferred from the file extension.
CreateFormFile from multipart Writer sets the content type always to `application/octet-stream`. If you need to preserve the content type of the file decided by its file extension, thi spackage will help you.
Files can be written using their path by a convenience method:
message := &bytes.Buffer{} writer := multipart.NewWriter(message) err := helpers.WriteFile(writer, "file", "test.txt") err := writer.Close()
Example ¶
package main import ( "bytes" "log" "mime/multipart" helpers "github.com/prantlf/go-multipart-helpers" "github.com/prantlf/go-multipart-helpers/demo" ) func main() { // Create a new buffer for the message content. message := &bytes.Buffer{} // Create a new multipart message writrer with a random boundary. writer := multipart.NewWriter(message) // Write a textual field. if err := writer.WriteField("comment", "a comment"); err != nil { log.Fatal(err) } // Write a file. if err := helpers.WriteFile(writer, "file", "demo/test.txt"); err != nil { log.Fatal(err) } // Finalize rhe message by appending the trailing boundary separatore. if err := writer.Close(); err != nil { log.Fatal(err) } // Make a network request with the composed content type and request body. demo.PrintRequest(writer.FormDataContentType(), message) }
Output: Content-Type: multipart/form-data; boundary=1879bcd06ac39a4d8fa5 Content-Length: 383 --1879bcd06ac39a4d8fa5 Content-Disposition: form-data; name="comment" a comment --1879bcd06ac39a4d8fa5 Content-Disposition: form-data; name="file"; filename="test.txt" Content-Type: text/plain; charset=utf-8 text file content --1879bcd06ac39a4d8fa5--
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CreateFilePart ¶
CreateFilePart creates a new form-data header with the provided field name, file name and its content type inferred from its file extension. It calls CreatePart to create a new multipart section with the provided header. The content of the file should be written to the returned Writer.
Example ¶
package main import ( "bytes" "log" "mime/multipart" helpers "github.com/prantlf/go-multipart-helpers" "github.com/prantlf/go-multipart-helpers/demo" ) func main() { message := &bytes.Buffer{} writer := multipart.NewWriter(message) // Create an empty file part. if _, err := helpers.CreateFilePart(writer, "file", "test.txt"); err != nil { log.Fatal(err) } writer.Close() demo.PrintRequest(writer.FormDataContentType(), message) }
Output: Content-Type: multipart/form-data; boundary=1879bcd06ac39a4d8fa5 Content-Length: 241 --1879bcd06ac39a4d8fa5 Content-Disposition: form-data; name="file"; filename="test.txt" Content-Type: text/plain; charset=utf-8 --1879bcd06ac39a4d8fa5--
func WriteFile ¶
WriteFile calls CreateFilePart and then writes the file content to the part writer.
Example ¶
package main import ( "bytes" "log" "mime/multipart" helpers "github.com/prantlf/go-multipart-helpers" "github.com/prantlf/go-multipart-helpers/demo" ) func main() { message := &bytes.Buffer{} writer := multipart.NewWriter(message) // Write a file. if err := helpers.WriteFile(writer, "file", "demo/test.bin"); err != nil { log.Fatal(err) } writer.Close() demo.PrintRequest(writer.FormDataContentType(), message) }
Output: Content-Type: multipart/form-data; boundary=1879bcd06ac39a4d8fa5 Content-Length: 259 --1879bcd06ac39a4d8fa5 Content-Disposition: form-data; name="file"; filename="test.bin" Content-Type: application/octet-stream binary file content --1879bcd06ac39a4d8fa5--
func WriteFileReader ¶
WriteFileReader calls CreateFilePart and then writes the reader content to the part writer.
Example ¶
package main import ( "bytes" "log" "mime/multipart" "strings" helpers "github.com/prantlf/go-multipart-helpers" "github.com/prantlf/go-multipart-helpers/demo" ) func main() { message := &bytes.Buffer{} writer := multipart.NewWriter(message) // Write a file. if err := helpers.WriteFileReader(writer, "file", "test.txt", strings.NewReader("test")); err != nil { log.Fatal(err) } writer.Close() demo.PrintRequest(writer.FormDataContentType(), message) }
Output: Content-Type: multipart/form-data; boundary=1879bcd06ac39a4d8fa5 Content-Length: 245 --1879bcd06ac39a4d8fa5 Content-Disposition: form-data; name="file"; filename="test.txt" Content-Type: text/plain; charset=utf-8 test --1879bcd06ac39a4d8fa5--
Types ¶
This section is empty.