使用postman模拟浏览器发送请求给服务端,服务端处理并返回结果的过程
1、服务端处理代码:
const http = require('http')
const server = http.createServer((req,res) => {
if (req.method === 'POST') {
// req 数据格式
console.log('req content-type: ', req.headers['content-type'])
// 接收数据
let postData = ''
req.on('data', chunk => {
postData += chunk.toString()//因为chunk是二进制格式,需要toString转换成字符串格式
})
req.on('end', () => {
console.log('postData: ',postData)
res.end('hello world !')
})
}
})
server.listen(8000)
console.log('OK')
2、写上url地址,如下:
3、左边下拉框选择请求的类型(以POST请求为例),如下:
4、选择“Body”,选择“raw”,如下:
5、右边下拉框选择请求的文件类型(以JSON类型为例,这是最常用的类型),如下:
6、在下面写一个符合JSON格式的代码,然后点击右边的Send发送到服务端,如下:
7、成功后下面会显示服务端返回的结果,如下:
8、此时,git控制台上打印出相关的内容(这是我服务端代码成功执行的结果),如下:
以上就是postman的使用,无需注册账号!