The difference between different data types of the interface and how the front-end (react+TS) handles the path request, body request or query request type interface

First, let’s talk about the difference between request data types:
multipart/form-data: You can upload files or key-value pairs, which will be converted into a message in the end.
x-www-form-urlencoded: Only key-value pairs can be uploaded, and key-value pairs are separated by & intervals.
Raw corresponds to input parameters in any format, and you can upload [text] in any format, such as text, json, xml, html, etc.

1. Processing method of path request type
Interface 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'
    }
  );
};

Business processing:

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. The processing method of query request type
Interface api:
insert image description here

import request from '@/utils/request';

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

Business processing: how many parameters are needed to list how many

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

3. The processing method of body request type
api interface:

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

1) List all parameters {}
2) An object wraps all parameter names, expand operator...object
3) Combination and matching

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

Note: If the requested data type is multipart/form-data, formdata must be used for business processing.

Guess you like

Origin blog.csdn.net/qq_37967853/article/details/129579237