Axios Request Config

Request Config

这些是用于发出请求的可用配置选项。只需要输入“url”。如果' method '未指定,则请求将默认为' GET '。

{
  url: '/user',
  method: 'get', 
  baseURL: 'https://some-domain.com/api/',
  headers: {'X-Requested-With': 'XMLHttpRequest'},
    
  params: {ID: 12345,Name:"Alex"},		// URL后的参数:.../?ID=12345&Name="Alex"
  data: { firstName: 'Fred'},			// 对象形式的data -> json 格式字符串
  data: 'Country=Brasil&City=Belo Horizonte',		// 表单形式
  paramsSerializer: {					// 对请求的参数进行序列化转化为字符串(符合服务端的路由匹配规则)
    encode?: (param: string): string => { }, 
    serialize?: (params: Record<string, any>, options?: ParamsSerializerOptions ),
    indexes: false 
  },
  formSerializer: {
      visitor: (value, key, path, helpers) => {}; 	// 自定义访问者函数来序列化表单值
      dots: boolean; 								// 使用圆点代替括号格式
      metaTokens: boolean;							// 在参数key中保留特殊的结尾,如{}
      indexes: boolean; 				// 数组索引格式为null -无括号,false -空括号,true -带索引的括号
  },

  transformRequest: [function (data, headers) {return data;}],	// 先对需要发送的数据进行处理,再发送(发送预处理)
  transformResponse: [function (data) {return data;}],			// 先对相应的数据进行处理,再获取(响应预处理)

  timeout: 1000, 						// 默认是0,不存在超时, 超时取消当前请求

  responseType: 'json',			// 返回值默认JSON格式
  responseEncoding: 'utf8',     // 返回编码格式默认UTF-8
    
  xsrfCookieName: 'XSRF-TOKEN', 	// 跨站请求标识(设置cookie名称), 类似django 中的 csrftoken
  xsrfHeaderName: 'X-XSRF-TOKEN', 	// 跨站请求头信息(设置header名称)
  withCredentials: false, 			// 默认false, 跨域请求时对cookie携带的设置,false表示不携带cookie
  cancelToken: new CancelToken(function (cancel) {}),   // CancelToken指定一个可用于取消请求的取消令牌(取消 ajax)

  onUploadProgress: function ({loaded, total, progress, bytes, estimated, rate, upload = true}) {},		// 上传时回调
  onDownloadProgress: function ({loaded, total, progress, bytes, estimated, rate, download = true}) {},	// 下载时回调
    
  maxContentLength: 2000,		// 内容最大尺寸
  maxBodyLength: 2000,			// 身体最大尺寸
  maxRedirects: 21, 			// 重定向最多 21

  httpAgent: new http.Agent({ keepAlive: true }),		// 对客户端信息的设置 http
  httpsAgent: new https.Agent({ keepAlive: true }),     // 对客户端信息的设置 https

  auth: {...}, 		// 基础验证
  decompress: true 	// 默认解压, 需不需要对响应结果进行解压
  insecureHTTPParser: undefined // 默认undefined, 不安全的HTTP解析器  
         
  validateStatus: function (status) {return status >= 200 && status < 300; }, // 校验什么状态算是成功的
  beforeRedirect: (options, { headers }) => {			// 跳转前处理(跳转预处理)
    if (options.hostname === "example.com") {
      options.auth = "user:password";
    }
  },  

  // 默认值NULL, 例如‘/var/run/docker.sock‘将请求发送到Docker守护进程. 同时设置socketPath && proxy 优先级 socketPath > proxy
  socketPath: null, 	 
  proxy: {
  	protocol: 'https',
  	host: '127.0.0.1',
  	port: 9000,
  	auth: {
  		username: 'mikeymike',
  		password: 'rapunz3l'
  	}
  },   
  transitional: {
    silentJSONParsing: true,		// 默认true, 忽略JSON解析错误并设置响应。如果解析失败,数据为空(旧行为)
    forcedJSONParsing: true,		// 默认true, 尝试将响应字符串解析为JSON,即使' responseType '不是' JSON '
    clarifyTimeoutError: false,		// 默认false, 在请求超时时抛出ETIMEDOUT错误,而不是通用的ECONNABORTED
  },
  env: {
    FormData: window?.FormData || global?.FormData	// 用于将有效负载自动序列化到FormData对象的FormData类
  },
  adapter: function (config) {},					// 对请求的适配器(adapter)进行设置:1.ajax;2.js中发送http
  maxRate: [										// 仅HTTP适配器
    100 * 1024, 	// 上传限速 100KB/s
    100 * 1024 		// 下载限速 100KB/s
  ] 
}

常用的参数

1.url
2.method
3.baseURL
4.headers
5.params
6.data
7.timeout
8.proxy
posted @ 2022-11-16 18:40  Redskaber  阅读(541)  评论(0)    收藏  举报