英文:
I don't know how to use data sent from flutter in golang
问题
以下是将数据从Flutter传输到Golang的逻辑代码:
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"mime/multipart"
"net/http"
"os"
)
type Pages struct {
Order uint `json:"order"`
Description string `json:"description"`
Images []string `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 []Tags `json:"tag_list"`
Pages []Pages `json:"pages"`
}
func handleRequest(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var data GetData
err = json.Unmarshal(body, &data)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// 处理接收到的数据
fmt.Println("Received Data:")
fmt.Println("Title:", data.Title)
fmt.Println("Date:", data.Date)
fmt.Println("Location:", data.Location)
fmt.Println("Address:", data.Address)
fmt.Println("Tags:")
for _, tag := range data.Tags {
fmt.Println("Tag ID:", tag.TagId)
fmt.Println("Tag Name:", tag.TagName)
}
fmt.Println("Pages:")
for _, page := range data.Pages {
fmt.Println("Order:", page.Order)
fmt.Println("Description:", page.Description)
fmt.Println("Images:")
for _, image := range page.Images {
fmt.Println("Image Path:", image)
// 将文件数据保存到S3或其他存储服务
// 这里只是示例,你需要根据实际情况进行处理
saveToS3(image)
}
}
// 返回响应
w.WriteHeader(http.StatusOK)
w.Write([]byte("Data received successfully"))
}
func saveToS3(filePath string) {
// 将文件保存到S3的逻辑代码
// 这里只是示例,你需要根据实际情况进行处理
fmt.Println("Saving file to S3:", filePath)
}
func main() {
http.HandleFunc("/test", handleRequest)
http.ListenAndServe(":8080", nil)
}
上述代码是一个简单的示例,它接收来自Flutter的数据并进行处理。你可以根据实际需求修改处理数据的逻辑,比如将文件保存到S3或其他存储服务中。请注意,这只是一个示例,你需要根据实际情况进行适当的修改和处理。
英文:
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.
答案1
得分: 1
调用Request.FormValue和Request.FormFile来获取值和文件。
对于类似数组的字段,生成与客户端相同的字符串参数名称。循环遍历数组索引,并在页面没有该字段时跳出循环。
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))
...
}
英文:
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))
...
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论