c.query("string")查询不到内容?

95 阅读2分钟

问题描述

写Vue3+Gin代码,c.query("")查了半天没数据,

直接上代码

Vue3部分代码

axios({
    headers:{
      'Content-Type':'application/x-www-form-urlencoded'
    },
    method: 'POST',
    url: "http://127.0.0.1:3000/socialnetwork/user/register/",
    data: {
        'username': username.value,
        'password': password_1.value,
    }
    }).then(response => {
        console.log("response: ", response)
        if(response.data.code == 200){
            console.log(response)
            router.push({name:"home_index"});
        }
        else{
          username.value = "";
          password_1.value = "";
          password_2.value = "";
          console.log(response)
        }
    });

使用apifox发送请求 image.png Gin正确收到了参数 image.png 控制变量法得出的结果就是,axios什么地方错了

image.png

执行代码

log.Println("context: ", c.Request)

打印内容

&{POST /socialnetwork/user/register/ HTTP/1.1 1 1 
map[Accept:[application/json, text/plain, */*] 
Accept-Encoding:[gzip, deflate, br] 
Accept-Language:[zh-CN,zh;q=0.9] 
Connection:[keep-alive] 
Content-Length:[28] 
Content-Type:[application/x-www-form-urlencoded] 
Origin:[http://localhost:8080]
Referer:[http://localhost:8080/] 
Sec-Ch-Ua:["Not_A Brand";v="8", "Chromium";v="120", "Google Chrome";v="120"] Sec-Ch-Ua-Mobile:[?0] 
Sec-Ch-Ua-Platform:["Windows"] 
Sec-Fetch-Dest:[empty] 
Sec-Fetch-Mode:[cors] 
Sec-Fetch-Site:[cross-site] 
User-Agent:[Mozilla/5.0 (WindowsNT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36]] 0xc0000f0100 <nil> 28 [] false 127.0.0.1:3000 map[] m
ap[] <nil> map[] 127.0.0.1:13930 /socialnetwork/user/register/ <nil> <nil> <nil> 0xc0000f0140}

apifox发送的

 &{POST /socialnetwork/user/register/?username=002&password=123456 HTTP/1.1 1 1 
 map[Accept:[*/*]
 Accept-Encoding:[gzip, deflate, br] 
 Connection:[keep-alive] 
 Content-Length:[0] 
 Content-Type:[application/json] 
 User-Agent:[Apifox/1.0.0 (https://www.apifox.cn)]] {} <nil> 0 [] false 127.0.0.1:3000 map[] map[] <nil> map[] 127.0.0.1:3681 /socialnetwork/user/register/?username=002&password=123456 <nil> <nil> <nil> 0xc000468bc0}

将axios中的数据从data中取出,放在param中

 &{POST /socialnetwork/user/register/?username=003&password=123456 HTTP/1.1 1 1 map[Accept:[application/json, text/plain, */*] Accept-Encoding:[gzip, deflate, br] Accept-Language:[zh-CN,zh;q=0.9] Connection:[keep-alive] Content-Length:[0] Origin:[http://localhost:8080] Referer:[http://localhost:8080/] Sec-Ch-Ua:["Not_A Brand";v="8", "Chromium";v="120", "Google Chrome";v="120"] Sec-Ch-Ua-Mobile:[?0] Sec-Ch-Ua-Platform:["Windows"] Sec-Fetch-Dest:[empty] Sec-Fetch-Mode:[cors] Sec-Fetch-Site:[cross-site] User-Agent:[Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36]] {} <nil> 0 [] false 127.0.0.1:3000 map[] map[] <nil> map[] 127.0.0.1:10398
 /socialnetwork/user/register/?username=003&password=123456 <nil> <nil> <nil> 0xc0000a2140}

解决方案

application/w-www-form-urlencoded 失败

zhuanlan.zhihu.com/p/578416839