vue整合keycloak踩坑(状态码415,缺少表单参数:grant_type)

237 阅读1分钟

axios请求

    handleLogin() {
      axios.post(
        `https://keycloak.jueao.net/auth/realms/apisixtest/protocol/openid-connect/token`,
        queryString.stringify({
          grant_type: 'password',
          client_id: 'apisix-fe',
          password: 'password',
          username: 'username',
          client_secret: '8WtLKa0c6VPsGKgx8pkYTVhP3O4rwuGe',
          audience: 'apisix-fe'
        }),
        { headers: 
          { 'Content-Type': 'application/x-www-form-urlencoded' }
        }
      ).then(res => {
        console.log(res)
      })
    },

状态码415

Content-Type标头值设置为application/x-www-form-urlencoded

状态码400,缺少表单参数:grant_type

  • 安装querystring npm install querystringyarn add querystring

  • axios请求中的data: data: queryString.stringify(params)