Get the HTTP Response Body with Axios
In this post, we will give you information about Get the HTTP Response Body with Axios. Here we will give you detail about Get the HTTP Response Body with Axios And how to use it also give you a demo for it if it is necessary.
When you await
on an Axios request, you get back an Axios response. An Axios response is a POJO with several
properties, including data
, which contains the parsed response body.
const axios = require('axios');
const res = await axios.get('https://httpbin.org/get', { params: { answer: 42 } });
res.constructor.name; // 'Object', means 'res' is a POJO
// 'res.data' contains the parsed response body
res.data; // { args: { answer: 42 }, ... }
res.data instanceof Object; // true
An Axios response contains several other properties, like status
, which contains the HTTP response status
code (like 200
or 404
). But most of the time you don’t care about the response code if the request
succeeded, so you will often see code that gets the response body directly using promise chaining.
const data = await axios.get(url).then(res => res.data);
You can also get the response body using destructuring assignments.
// Equivalent to 'const data = await axios.get(url).then(res => res.data)'
const { data } = await axios.get(url);
Automatic Parsing
Axios parses the response based on the HTTP response’s Content-Type
header. When the response’s content type
is application/json
, Axios will automatically try to parse the response into a JavaScript object.
const axios = require('axios');
const res = await axios.get('https://httpbin.org/get', { params: { answer: 42 } });
res.headers['content-type']; // 'application/json'
Keep in mind that the response headers are sent by the server. So if the server sends back a different content
type, you may need to handle it the response yourself.
For other content types, like text/html
, the res.data
property will be a string.
const axios = require('axios');
const res = await axios.get('https://httpbin.org/html');
res.headers['content-type']; // 'text/html; charset=utf-8'
typeof res.data; // 'string'
res.data; // '... <h1>Herman Melville - Moby-Dick</h1> ...'
Streaming for Get the HTTP Response Body with Axios
You can configure the type of the data
property using Axios’ responseType
object. By default,
responseType
is set to 'json'
, which means Axios will try to parse the response as JSON.
However, that isn’t correct if you’re looking to, say, download an image using Axios. You can set responseType
to 'arraybuffer'
to get the response as an ArrayBuffer:
const axios = require('axios');
const res = await axios.get('https://images.unsplash.com/photo-1506812574058-fc75fa93fead', {
responseType: 'arraybuffer'
});
const fs = require('fs');
fs.writeFileSync('./south-beach.jpg', res.data);
You can also set responseType
to 'stream'
to get the response as a Node.js stream:
const axios = require('axios');
const res = await axios.get('https://images.unsplash.com/photo-1506812574058-fc75fa93fead', {
responseType: 'stream'
});
const fs = require('fs');
res.data.pipe(fs.createWriteStream('./south-beach.jpg'));
Axios is a promise-based HTTP client for the browser and node.js. It is a small library that makes it easy to make HTTP requests and get responses. Axios supports all the major browsers and node.js versions.
Here are some of the features of Axios:
- Promise-based: Axios uses promises to return the results of HTTP requests. This makes it easy to chain requests and handle errors.
- Cross-platform: Axios works in the browser and node.js. This makes it easy to use Axios for both frontend and backend development.
- Extensible: Axios is highly extensible. You can use it to make custom HTTP requests and handle custom responses.
The response data will be a JSON object that contains the user’s name, email address, and other information. Axios is a powerful tool that can be used to make HTTP requests in the browser and node.js. It is easy to use and extensible, making it a great choice for a variety of projects.
Hope this code and post will help you for implementing Get the HTTP Response Body with Axios. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs