受け取る際のメモです。
ボディで渡すパラメータは JSON 形式を想定します。
環境
express:4.18.2
typescript:5.1.6
例
express 側のコード
import express from 'express';
const app = express();
app.use(express.json());
app.put('/test/:id', async (req, res) => {
const {
a,
b,
} = req.query;
const {
name,
time,
} = req.body;
res.send({
id: req.params.id,
name,
time,
a,
b,
});
});
app.listen(8080, () => {
console.log('listening on port 8080');
});
呼ぶ側で VSCode の機能拡張 REST Client を使った場合
PUT http://localhost:8080/test/1024?a=鈴木&b=花子
content-type: application/json
{
"name": "山田太郎",
"time": "2023-08-05T00:00:00Z"
}
実行結果
HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: application/json; charset=utf-8
Content-Length: 91
ETag: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Date: Fri, 05 Aug 2023 00:00:00 GMT
Connection: close
{
"id": "1024",
"name": "山田太郎",
"time": "2023-08-05T00:00:00Z",
"a": "鈴木",
"b": "花子"
}
コメント