Created
June 18, 2019 20:19
-
-
Save uguisu-an/db7da94de1150f51e3d3c3a6bada9734 to your computer and use it in GitHub Desktop.
POST application/json or application/x-www-form-urlencoded, multipart/form-data with axios and expressjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import axios from "axios"; | |
// application/json | |
axios.post("http://localhost:3000/", { | |
a: 1, | |
b: "hello", | |
c: new Date() | |
}); | |
// application/x-www-form-urlencoded | |
const params = new URLSearchParams(); | |
params.append("a", "2"); | |
params.append("b", "hello"); | |
params.append("c", new Date().toISOString()); | |
axios.post("http://localhost:3000/", params); | |
// multipart/form-data | |
const data = new FormData(); | |
data.append("a", "3"); | |
data.append("b", "hello"); | |
data.append("c", new Date().toISOString()); | |
axios.post("http://localhost:3000/", data); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require("express"); | |
const app = express(); | |
const cors = require("cors"); | |
app.use(cors({ origin: "*" })); | |
const bodyParser = require("body-parser"); | |
app.use(bodyParser.json()); | |
app.use(bodyParser.urlencoded({ extended: true })); | |
const multer = require("multer"); | |
app.use(multer().none()); | |
app.post("/", (req, res) => { | |
console.info(req.headers["content-type"]); | |
console.info(req.body); | |
}) | |
app.listen(3000); | |
console.info("http://localhost:3000/"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment