【问题标题】:Making a x-www-form-urlencoded request with axios使用 axios 发出 x-www-form-urlencoded 请求
【发布时间】:2021-01-22 13:36:59
【问题描述】:
const { user } = require('./config');
const axios = require('axios');

const Querystring = require('querystring');

let body = Querystring['stringify']({
    email: 'MY EMAIL@email.com',
    password: 'pass'
})

const config = {
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded'
  }
}

axios['post']('https://minecraftservers.org/login', body, config)
['then'](response => console.log(response))

我正在尝试通过网站登录 它没有 api 标题是正确的 如果你在徘徊我是怎么知道的,我使用了 chrome 开发工具 像逆向工程师

content-type: application/x-www-form-urlencoded

这是我尝试登录网站时他们使用的标题

这是我通过网站而不是代码登录时得到的,它在那里工作。

【问题讨论】:

  • 你可以试试下面的。 axios.request ({ url: '/user', method: 'post', data: { email: 'MY EMAIL@email.com', password: 'pass' }, headers: {'Content-Type':'application /x-www-form-urlencoded'}, // ... 和其他选项 })/.then(response=>console.log(response))

标签: javascript node.js


【解决方案1】:

您可以使用URLSearchParams

const params = new URLSearchParams();
params.append('firstName', 'paul');
params.append('lastName', 'fred');
axios.post('/user', params);

它避免添加另一个库。

【讨论】:

    【解决方案2】:

    我猜系统税是你的问题。除了语法之外,你还有什么困难吗?

    const { user } = require('./config');
    const axios = require('axios');
    
    const Querystring = require('querystring');
    
    let body = Querystring['stringify']({
        email: 'MY EMAIL@email.com',
        password: 'pass'
    })
    
    const config = {
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      }
    }
    
    axios.post('https://minecraftservers.org/login', body, config)
    .then(response => console.log(response))
    

    【讨论】:

    • 如果有人现在想使用此代码。 Querystring API 是旧版。使用 URLSearchParams()。
    【解决方案3】:

    试试

    axios.post('https://minecraftservers.org/login', body, config)
    .then(response => console.log(response))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-30
      • 2018-12-17
      • 2018-12-25
      • 2018-03-24
      • 1970-01-01
      相关资源
      最近更新 更多