接口不同数据类型的区别及前端(react+TS)对于path请求、body请求或query请求类型接口的处理方式

首先谈下请求数据类型的区别:
multipart/form-data:可以上传文件或者键值对,最后都会转化为一条消息。
x-www-form-urlencoded:只能上传键值对,而且键值对都是通过&间隔分开的。
raw对应的是入参是任意格式的可以上传任意格式的【文本】,可以上传text、json、xml、html等。

1.path请求类型的处理方式
接口api:

import request from '@/utils/request';

//查询气象站历史温度
export interface MeteorologicalInquiryParams {
    
    
  deviceId: String;
  begin?: Number;//?代表可选
  end?: Number;
}
export const getWeatherInfoHistory = ({
     
      deviceId, begin, end }: MeteorologicalInquiryParams) => {
    
    
  return request(
    `/api-telematics-pc/v1/agriconditionmonitor/getWeatherInfoHistory/${
      
      deviceId}/${
      
      begin}/${
      
      end}`,
    {
    
    
      method: 'get'
    }
  );
};

业务处理:

import {
    
     getWeatherInfoHistory } from './service';
import type {
    
     MeteorologicalInquiryParams } from './service';

  useEffect(() => {
    
    
    if (startdate && enddate) {
    
    
      let params: MeteorologicalInquiryParams = {
    
    
        deviceId: deviceid,
        begin: Number(startdate),
        end: Number(enddate)
      };
      getWeatherInfoHistory(params).then((res) => {
    
    
           .......
      });
    }
  }, [daysrange, startdate, enddate]);

2.query请求类型的处理方式
接口api:
在这里插入图片描述

import request from '@/utils/request';

//获取设备类型
export const getCategoryMenuInfo = (params: any) => {
    
    
  return request(`/api-telematics-pc/v1/farmdevicemanager/getCategoryMenuInfo`, {
    
    
    method: 'get',
    params
  });
};

业务处理:需要几个参数列几个

let parentCategoryName='农情设备'
let result = await getCategoryMenuInfo({
    
     parentCategoryName});

3.body请求类型处理方式
api接口:

//新增当前农情设备信息
export const addDeviceInfo = (body: any) => {
    
    
  return request(`/api-telematics-pc/v1/farmdevicemanager/addDeviceInfo`, {
    
    
    method: 'post',
    body
  });
};

1)参数{}全部列出
2)某个对象包裹所有参数名字,展开运算符…object
3)组合搭配

addDeviceInfo({
    
    
          companyId,
          farmCode,
          categoryName,
          deviceName,
          id,
          imageUrls,
          landCode,
          latitude,
          longitude,
          serialNum,
          statusValue,
          modelNum,
          parentId,
          producer
        }).then((res) => {
    
    }

注意:若请求数据类型为multipart/form-data,业务处理时需用formdata进行包裹。

猜你喜欢

转载自blog.csdn.net/qq_37967853/article/details/129579237
今日推荐