How to retrieve a POST request using WebAPI?
You can retrieve a POST request from a web API by using one of the following methods:
Retrieve a POST request using the Express framework in Node.js.
const express = require('express');
const app = express();
app.use(express.json()); // 解析JSON请求体
app.use(express.urlencoded({ extended: true })); // 解析URL编码的请求体
app.post('/api/endpoint', (req, res) => {
const requestBody = req.body; // 获取请求体数据
// 处理请求
res.send('POST请求已成功处理');
});
app.listen(3000, () => {
console.log('Web API正在监听端口3000');
});
2. Retrieve POST requests using the Flask framework in Python.
from flask import Flask, request
app = Flask(__name__)
@app.route('/api/endpoint', methods=['POST'])
def handle_post_request():
request_data = request.get_json() # 获取请求体数据
# 处理请求
return 'POST请求已成功处理'
if __name__ == '__main__':
app.run()
Regardless of which language or framework you’re using, you can access the POST request body data in the corresponding route handler using the request.body or request.get_json() method.