簡體   English   中英

使用 axios 發出 x-www-form-urlencoded 請求

[英]Making a x-www-form-urlencoded request with axios

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

這是他們在我嘗試登錄該站點時使用的標題

這是我通過網站登錄時得到的,而不是代碼,它在那里工作。 圖片

您可以使用URLSearchParams

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

它避免了添加另一個庫。

我猜 systax 是你的問題。 除了語法,你還有什么困難嗎?

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))

嘗試

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM