LoginSignup
118
94

More than 5 years have passed since last update.

fetch API でも POST したい!

Posted at

fetch API でも POST したい!

ちなみに fetch API は WHATWG HTML Living Standard で定義されていて、W3C HTML 5.1 でもなければ ECMAScript2017 でもないです。

2017年現在 Safari や IE では未サポートなので https://github.com/github/fetch などの polyfill を使うと良いです。

post (application/x-www-form-urlencoded)

const obj = {hello: "world"};
const method = "POST";
const body = Object.keys(obj).map((key)=>key+"="+encodeURIComponent(obj[key])).join("&");
const headers = {
  'Accept': 'application/json',
  'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'
};
fetch("./new", {method, headers, body}).then((res)=> res.json()).then(console.log).catch(console.error);

post (multipart/form-data)

const obj = {hello: "world"};
const method = "POST";
const body = Object.keys(obj).reduce((o,key)=>(o.set(key, obj[key]), o), new FormData());
const headers = {
  'Accept': 'application/json'
};
fetch("./new", {method, headers, body}).then((res)=> res.json()).then(console.log).catch(console.error);

post (aplication/json)

const obj = {hello: "world"};
const method = "POST";
const body = JSON.stringify(obj);
const headers = {
  'Accept': 'application/json',
  'Content-Type': 'application/json'
};
fetch("./new", {method, headers, body}).then((res)=> res.json()).then(console.log).catch(console.error);
118
94
2

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
118
94