KevvyCodes
KevvyCodes

Reputation: 171

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 protected]',
    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))

Im trying to login through a website it doesn't have an api the headers are correct if you're wandering how i knew this, i used chrome dev tools like reverse engineer

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

that's the header they used when i tried to login to the site

this is what i get when i logged in through the site and not the code, it works there. image

Upvotes: 17

Views: 23835

Answers (3)

Archimedes Trajano
Archimedes Trajano

Reputation: 41290

You can use URLSearchParams

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

It avoids adding another library.

Upvotes: 37

GRS
GRS

Reputation: 1967

I guess systax is your problem. Do you have any difficulties other than the syntax?

const { user } = require('./config');
const axios = require('axios');

const Querystring = require('querystring');

let body = Querystring['stringify']({
    email: 'MY [email protected]',
    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))

Upvotes: 5

Anubhav Das
Anubhav Das

Reputation: 1039

Try

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

Upvotes: -1

Related Questions