Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 649
KevvyCodes Avatar asked Mar 20 '19 09:03

KevvyCodes


People also ask

How to send urlencoded data using Axios?

I had to figure out this problem: how to send urlencoded data using Axios? The first thing we need to do is to install the qs module. It’s a cool querystring parsing and stringifying library with some added security: Then we need to import the qs module along with the Axios import, of course: Next, the Axios code.

How do I send an API request with Axios?

An API request can be sent in a variety of ways. We can use a command-line tool like cURL, the browser's native Fetch API, or a package like Axios to accomplish this. Sending HTTP requests to your API with Axios is a fantastic tool.

How do I get data from a server using Axios?

Axios can make a GET request to “get” data from a server. The axios.get () method is used to make an HTTP get request. There are two parameters that must be passed to the get () method.

How do I use the full form of Axios in QS?

In short, we need to use the full form for the Axios request. Not axios.post () but axios (). Inside there, we use the stringify () method provided by qs and we wrap the data into it. We then set the content-type header:


2 Answers

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.

like image 57
Archimedes Trajano Avatar answered Oct 17 '22 06:10

Archimedes Trajano


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))
like image 29
Ganesh Ravi Shankar Avatar answered Oct 17 '22 07:10

Ganesh Ravi Shankar