Goframe因为axios的header导致的一个BUG解析
服务器搭建:
go mod init test
go mod edit -require github.com/gogf/gf@latest
package main import ( "fmt" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/net/ghttp" ) func main() { s := g.Server() s.Group("/api", func(group *ghttp.RouterGroup) { group.Middleware(MiddlewareCORS) // 后台模块 group.ALL("/test", List) }) s.Run() } // MiddlewareCORS 允许跨域 func MiddlewareCORS(r *ghttp.Request) { r.Response.CORSDefault() r.Middleware.Next() } func List(r *ghttp.Request) { fmt.Println(r.Get("name")) r.Response.Write("hello world") }
启动服务
前端使用anxis
main.js
package.json
webpack.config.js
import Vue from 'vue'; import axios from 'axios'; import VueAxios from 'vue-axios'; Vue.use(VueAxios, axios) // Vue.axios.get("http://localhost/api/test").then((response) => { // console.log(response.data) // }) axios({ headers: { // application/json : 请求体中的数据会以json字符串的形式发送到后端 // application/x-www-form-urlencoded:请求体中的数据会以普通表单形式(键值对)发送到后端 // multipart/form-data: 它会将请求体的数据处理为一条消息,以标签为单元,用分隔符分开。既可以上传键值对,也可以上传文件。 'Content-Type': 'multipart/form-data' //参数为object时候请求体中的数据会以普通表单形式(键值对)发送到后端 }, method: 'post', url: 'http://localhost/api/test', data: { name: { 'test': 'hello' }, } });
以上请求将会直接panic :
Invalid Request: no multipart boundary param in Content-Type
const path = require('path'); module.exports = { entry: './main.js', output: { path: path.resolve(__dirname, 'dist'), filename: 'main.bundle.js', }, };
{ "scripts": { "build": "webpack" }, "dependencies": { "axios": "^0.24.0", "vue-axios": "^3.4.0", "webpack": "^5.4.0", "webpack-cli": "^4.2.0" } }
执行
npm install
npm run build
生成 main.bundle.js
在浏览器console执行
发送请求;
以上需要搞懂:
1、What is the boundary in multipart/form-data?
https://stackoverflow.com/questions/3508338/what-is-the-boundary-in-multipart-form-data?answertab=votes#tab-top
2、axios使用multipart/form-data方式上传文件?
https://github.com/wangsijie/blog/issues/67
3、
I can see a bigger world.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示
2021-01-11 关于vue的$refs出现undefined
2021-01-11 关于vue表单Element表单验证事件初次加载就触发了校验的问题 - 如何清除验证结果