506
401

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

axiosのパラメータ指定方法まとめ

Posted at

概要

HTTPクライアントのaxiosを使っていて、
パラメータの指定する方法を毎回調べるのが面倒だったのでまとめました。
ほぼREADMEに書いてある内容と同じです。

GET

クエリパラメータをつける

直接URLにつける

axios.get('/user?ID=12345');

optionに指定する

axios.get('/user', {
  params: {
    // ここにクエリパラメータを指定する
    ID: 12345
  }
});

POST

json形式で送る

axios.post('/user', {
    firstName: 'Fred',
    lastName: 'Flintstone'
});

application/x-www-form-urlencoded形式で送る

URLSearchParams を使う方法

var params = new URLSearchParams();
params.append('param1', 'value1');
params.append('param2', 'value2');
axios.post('/foo', params);

qs を使う方法

var qs = require('qs');
axios.post('/foo', qs.stringify({ 'bar': 123 }));
506
401
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
506
401

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?