tech.chakapoko.com
Home / Node.js / HTTP

[Node.js]axiosの使い方

package.json

{
  "dependencies": {
    "axios": "^0.20.0"
  }
}

GET リクエスト

const axios = require("axios").default;

axios
  .get("https://postman-echo.com/get?foo1=bar1&foo2=bar2")
  .then((response) => console.log(response.data))
  .catch(console.log);

実行結果:

{
  args: { foo1: 'bar1', foo2: 'bar2' },
  headers: {
    'x-forwarded-proto': 'https',
    'x-forwarded-port': '443',
    host: 'postman-echo.com',
    'x-amzn-trace-id': 'Root=1-5f5b39d5-fa615ade7bb95c72b64616b0',
    accept: 'application/json, text/plain, */*',
    'user-agent': 'axios/0.20.0'
  },
  url: 'https://postman-echo.com/get?foo1=bar1&foo2=bar2'
}

POST リクエスト (JSON)

const axios = require("axios").default;

axios
  .post("https://postman-echo.com/post", {
    foo1: "bar1",
    foo2: "bar2",
  })
  .then((response) => console.log(response.data))
  .catch(console.error);

実行結果:

{
  args: {},
  data: { foo1: 'bar1', foo2: 'bar2' },
  files: {},
  form: {},
  headers: {
    'x-forwarded-proto': 'https',
    'x-forwarded-port': '443',
    host: 'postman-echo.com',
    'x-amzn-trace-id': 'Root=1-5f5b3a25-b2f56a190f8e511744bb0efc',
    'content-length': '29',
    accept: 'application/json, text/plain, */*',
    'content-type': 'application/json;charset=utf-8',
    'user-agent': 'axios/0.20.0'
  },
  json: { foo1: 'bar1', foo2: 'bar2' },
  url: 'https://postman-echo.com/post'
}

POST リクエスト (application/x-www-form-urlencoded)

axios での POST リクエストはデフォルトでは Content-Type がapplication/json で送信されます。

application/x-www-form-urlencoded で送信するには URLSearchParams を利用します。

const axios = require("axios").default;

const params = new URLSearchParams();
params.append("param1", "value1");
params.append("param2", "value2");

axios
  .post("https://postman-echo.com/post", params)
  .then((response) => console.log(response.data))
  .catch(console.error);

実行結果:

{
  args: {},
  data: '',
  files: {},
  form: { param1: 'value1', param2: 'value2' },
  headers: {
    'x-forwarded-proto': 'https',
    'x-forwarded-port': '443',
    host: 'postman-echo.com',
    'x-amzn-trace-id': 'Root=1-5f5b3859-5ebd0fe8fb04404603ef38d4',
    'content-length': '27',
    accept: 'application/json, text/plain, */*',
    'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
    'user-agent': 'axios/0.20.0'
  },
  json: { param1: 'value1', param2: 'value2' },
  url: 'https://postman-echo.com/post'
}

POST リクエスト (multipart/form-data, ファイルアップロード)

$ npm install form-data
const FormData = require("form-data");
const fs = require("fs");
const axios = require("axios").default;

const form = new FormData();
form.append("foo1", "bar1");
form.append("foo2", "bar2");
form.append("foo3", fs.createReadStream("a.png"));

axios
  .post("https://postman-echo.com/post", form, {
    headers: form.getHeaders(),
  })
  .then((response) => console.log(response.data))
  .catch(console.error);

実行結果:

{
  args: {},
  data: {},
  files: {
    'a.png': 'data:application/octet-stream;base64,(省略)
  },
  form: { foo1: 'bar1', foo2: 'bar2' },
  headers: {
    'x-forwarded-proto': 'https',
    'x-forwarded-port': '443',
    host: 'postman-echo.com',
    'x-amzn-trace-id': 'Root=1-5f5b5701-640e8398db8a56988c225cb0',
    'transfer-encoding': 'chunked',
    accept: 'application/json, text/plain, */*',
    'content-type': 'multipart/form-data; boundary=--------------------------287400157132457220999912',
    'user-agent': 'axios/0.20.0'
  },
  json: null,
  url: 'https://postman-echo.com/post'
}

Related contents