如何使用postman传数组数据

如何使用postman传数组数据

  在我们做api接口数据调试的时候,大部分是会用到postman的,一般请求数据的参数都是字符串,但是特殊情况下我们是需要传一个数组数据的,那么为了实现这种需求,究竟该怎么做呢?请看下文

  看了网上的很多文档,试了一个网页的链接,没有一个实现我的需求,主要是解释文档重点模糊,导致读者不明所以,所以我又单独写这个文档出来释疑。请看重点

  method

    POST

  params

    name:测试鞋子

    description:测试描述

    imgs:【1,2】

  具体实现

  首先是headers需要进行设置Content-Type

如何使用postman传数组数据_第1张图片

 

  然后在body中选择raw、以及json(application/json),数据采用json格式

如何使用postman传数组数据_第2张图片

 

  postman本地返回结果

如何使用postman传数组数据_第3张图片

  服务器接收数据结果

如何使用postman传数组数据_第4张图片

  服务器端代码,无需关注,只是给真的用了mongodb数据库的看一下逻辑

const {
  ProductType
} = require('../../../modules');
const express = require('express');
const router = express.Router();

router.post('/imgs', async (req, res) => {
  const pt = ProductType(req.body);
  const result = await pt.save();
  res.json({
    status: 'success',
    info: result
  })
})

module.exports = router;

 

  下为前台页面代码

$.ajax({
        type:'post',
        url:'/api/v1/admin/add',
        data:{name:'ceshi',desc:'ceshi',imgs:[1,2,3,4]},
        traditional:true,
        success:function(data){
          console.log(data)
        }
})

 

  希望能够帮到大家!

  有用点个赞再走呗0,0

转载于:https://www.cnblogs.com/gitByLegend/p/10611192.html

你可能感兴趣的:(如何使用postman传数组数据)