var request = http.MultipartRequest('post', Uri.parse(TravelingUrl.testAddress '/test'));
request.fields['title'] = createDiary.value.title as String;
request.fields['tag_list'] = createDiary.value.tagList.toString();
request.fields['date'] = createDiary.value.date.toString();
for (var i = 0; i < createDiary.value.pages!.length; i ) {
Pages _tempPage = createDiary.value.pages![i];
request.fields['pages[$i][order]'] = jsonEncode(_tempPage.order);
request.fields['pages[$i][description]'] =
jsonEncode(_tempPage.description);
for (var ii = 0; ii < _tempPage.images!.length; ii ) {
request.files.add(await http.MultipartFile.fromPath(
'pages[$i][images]', _tempPage.images![ii]));
}
}
var response = await request.send();
The code above is the logic to transfer data from flutter to golang.
and i want use in golang what recived data from flutter.
i defined struct at golang like this
type _pages struct {
Order uint `json:"order"`
Description string `json:"description"`
Images []multipart.File `json:"images"`
}
type _tags struct {
TagId uint `json:"id"`
TagName string `json:"tag_name"`
}
type _getData struct {
Title string `json:"title"`
Date string `json:"date"`
Location string `json:"location"`
Address string `json:"address"`
_tags
_pages
}
I'm not familiar with golang, please help, can I see a simple example of getting data and using it?
There is file data in the array variable, I want to receive the data and save this file data to s3.
CodePudding user response:
Call Request.FormValue and Request.FormFile to get the values and files.
For the array-like fields, generate string parameter names as the client does. Loop though array indices and break when there is no field for the page.
title := r.FormValue("title")
tagList := r.FormValue("tag_list")
...
for i := 0; i < maxPossiblePages; i {
if _, ok := r.Form[fmt.Sprintf("pages[%d][order]", i)]; !ok {
break;
}
pageOrder := r.FormValue(fmt.Sprintf("pages[%d][order]", i))
pageDescription := r.FormValue(fmt.Sprintf("pages[%d][description]", i))
...
}