Hello,
i am morely new to _Go_ and build some things for my backend these days with net/http. Now, i tried fasthttp which works awesome - as known 馃槉 But, it's a very wide topic and a very complex API.
I want to work with uploaded POST form values and multipart/form-data, which seems confusing. In most languages and frameworks one can iterate over this data, like an _array_ or _map_ (practically without knowing all their names itself). I did not found some samples how to parse uploads in fasthttp.
Given is my _upload-script-dummy_ as simple function:
func upload(ctx *fasthttp.RequestCtx) {
// ...
}
This function in this case is called via https://domain.tld/upload with the following _dummy-forms_
<form action="upload" method="post">
<input type="text" name="name">
<input type="email" name="mail">
<textarea name="message"></textarea>
</form>
How to iterate over those _pairs_? (name=max, [email protected], message=Hi!)
<form method="upload" enctype="multipart/form-data">
<input name="file" type="file">
</form>
How do i get/save (move) this (temp-)file? (file=name.jpg)
How to access extended data, like size etc.?
<form method="upload" enctype="multipart/form-data">
<input name="files[]" type="file" multiple>
</form>
How to iterate over multiple files in files[]?
(files=[1=[name=a.jpg, size=12345, ...], 2=[name=b.png, size=...], 3=[name=c.txt, ...]])
How to access their extended data, like size, tempname or whatever is in there?
Can someone give me reduced examples (just the needed lines) to handle that?
Thank you very much 鈽猴笍
(i am happy too if it's only a sample for one of that 馃槉 )
Multipart forms are so easy to use in fasthttp. I think than are more easy than net/http. I won't provide you a full example only some utils functions.
You can use MultipartForm to interact with the multipart form (obviously).
form, err := ctx.MultipartForm()
if err != nil {
ctx.Error(...)
return
}
// We got the form!
Given the first html form we will make the following to get text, email and message (do not forget form.Value is a map[string][]string, so given a key the value will be a string slice):
texts := form.Value["text"]
if len(texts) == 0 {
ctx.Error(...)
}
text := texts[0]
emails := form.Value["email"]
if len(emails) == 0 {
ctx.Error(...)
}
email := emails[0]
...
Given the second form we will get the file using FormFile and store it in disk using SaveMultipartFile.
header, err := ctx.FormFile("file")
if err != nil {
ctx.Error(...)
}
fasthttp.SaveMultipartFile(header, fmt.Sprintf("/tmp/%s", header.Filename))
Given the third form we will get the files walking along form.Files map.
form, err := ctx.MultipartForm()
if err != nil {
ctx.Error(...)
return
}
for k, v := range form.File {
for _, header := range v {
fasthttp.SaveMultipartFile(header, fmt.Sprintf("/tmp/%s", header.Filename))
}
}
@dgrr thank you very much :100:
Most helpful comment
Multipart forms are so easy to use in fasthttp. I think than are more easy than net/http. I won't provide you a full example only some utils functions.
You can use MultipartForm to interact with the multipart form (obviously).
Given the first html form we will make the following to get
text,emailandmessage(do not forget form.Value is a map[string][]string, so given a key the value will be a string slice):Given the second form we will get the file using FormFile and store it in disk using SaveMultipartFile.
Given the third form we will get the files walking along form.Files map.