左本Web3D 左本Web3D
首页
  • Cesium案例

    • 地形|模型加载,地图绘制测量 (opens new window)
    • 基于若依三维Gis可视化系统 (opens new window)
  • Mapbox案例

    • 养殖场模型渲染 (opens new window)
    • 近海阀式养殖 (opens new window)
  • Three案例

    • 学习案例
    • 网络案例,附源码地址
  • Three+D3

    • Demo | D3.js SVG Maps and Three.js (opens new window)
    • Demo | MapBox with D3.js and Three.js (opens new window)
  • 项目案例

    • 消防楼物联网三维可视化大屏
    • 智慧养殖三维可视化大屏
    • 智慧农田三维可视化大屏
    • 智慧城市运营管理平台
    • 智慧设备运维综合管理平台
    • 设备运维管理系统
    • 网格工具
    • 博物馆VR全景
    • 数字虚拟展厅
  • 在线访问

    • 网格工具 (opens new window)
    • 博物馆VR全景 (opens new window)
    • AI数字人 (opens new window)
    • 水厂3D可视化 (opens new window)
    • 3D地图 (opens new window)
  • 链接工具

    • Web3D相关链接
    • Gis相关链接
  • 小游戏

    • 吃什么? (opens new window)
CSDN (opens new window)
Gitee (opens new window)
GitHub (opens new window)
备用地址 (opens new window)

左本Web3D

专注Web3D技术领域
首页
  • Cesium案例

    • 地形|模型加载,地图绘制测量 (opens new window)
    • 基于若依三维Gis可视化系统 (opens new window)
  • Mapbox案例

    • 养殖场模型渲染 (opens new window)
    • 近海阀式养殖 (opens new window)
  • Three案例

    • 学习案例
    • 网络案例,附源码地址
  • Three+D3

    • Demo | D3.js SVG Maps and Three.js (opens new window)
    • Demo | MapBox with D3.js and Three.js (opens new window)
  • 项目案例

    • 消防楼物联网三维可视化大屏
    • 智慧养殖三维可视化大屏
    • 智慧农田三维可视化大屏
    • 智慧城市运营管理平台
    • 智慧设备运维综合管理平台
    • 设备运维管理系统
    • 网格工具
    • 博物馆VR全景
    • 数字虚拟展厅
  • 在线访问

    • 网格工具 (opens new window)
    • 博物馆VR全景 (opens new window)
    • AI数字人 (opens new window)
    • 水厂3D可视化 (opens new window)
    • 3D地图 (opens new window)
  • 链接工具

    • Web3D相关链接
    • Gis相关链接
  • 小游戏

    • 吃什么? (opens new window)
CSDN (opens new window)
Gitee (opens new window)
GitHub (opens new window)
备用地址 (opens new window)
  • 初识 TypeScript

    • 简介
    • 安装 TypeScript
    • 编写第一个 TypeScript 程序
  • TypeScript 常用语法

    • 基础类型
    • 变量声明
    • 接口
    • 类
    • 函数
    • 泛型
    • 类型推断
    • 高级类型
  • ts-axios 项目初始化

    • 需求分析
    • 初始化项目
    • 编写基础请求代码
  • ts-axios 基础功能实现

    • 处理请求 url 参数
    • 处理请求 body 数据
    • 处理请求 header
      • 需求分析
      • processHeaders 函数实现
      • 实现请求 header 处理逻辑
      • demo 编写
    • 获取响应数据
    • 处理响应 header
    • 处理响应 data
  • ts-axios 异常情况处理

    • 错误处理
    • 错误信息增强
  • ts-axios 接口扩展

    • 扩展接口
    • axios 函数重载
    • 响应数据支持泛型
  • ts-axios 拦截器实现

    • 拦截器设计与实现
  • ts-axios 配置化实现

    • 合并配置的设计与实现
    • 请求和响应配置化
    • 扩展 create 静态接口
  • ts-axios 取消功能实现

    • 取消功能的设计与实现
  • ts-axios 更多功能实现

    • withCredentials
    • XSRF 防御
    • 上传和下载的进度监控
    • HTTP 授权
    • 自定义合法状态码
    • 自定义参数序列化
    • baseURL
    • 静态方法扩展
  • ts-axios 单元测试

    • 前言
    • Jest 安装和配置
    • 辅助模块单元测试
    • 请求模块单元测试
    • headers 模块单元测试
    • Axios 实例模块单元测试
    • 拦截器模块单元测试
    • mergeConfig 模块单元测试
    • 请求取消模块单元测试
    • 剩余模块单元测试
  • ts-axios 部署与发布

    • ts-axios 编译与发布
    • 引用 ts-axios 库
  • 《TypeScript 从零实现 axios》
  • ts-axios 基础功能实现
HuangYi
2020-01-05
目录

处理请求 header

# 处理请求 header

# 需求分析

我们上节课遗留了一个问题:

axios({
  method: 'post',
  url: '/base/post',
  data: {
    a: 1,
    b: 2
  }
})
1
2
3
4
5
6
7
8

我们做了请求数据的处理,把 data 转换成了 JSON 字符串,但是数据发送到服务端的时候,服务端并不能正常解析我们发送的数据,因为我们并没有给请求 header 设置正确的 Content-Type。

所以首先我们要支持发送请求的时候,可以支持配置 headers 属性,如下:

axios({
  method: 'post',
  url: '/base/post',
  headers: {
    'content-type': 'application/json;charset=utf-8'
  },
  data: {
    a: 1,
    b: 2
  }
})
1
2
3
4
5
6
7
8
9
10
11

并且在当我们传入的 data 为普通对象的时候,headers 如果没有配置 Content-Type 属性,需要自动设置请求 header 的 Content-Type 字段为:application/json;charset=utf-8。

# processHeaders 函数实现

根据需求分析,我们要实现一个工具函数,对 request 中的 headers 做一层加工。我们在 helpers 目录新建 headers.ts 文件。

helpers/headers.ts:

import { isPlainObject } from './util'

function normalizeHeaderName (headers: any, normalizedName: string): void {
  if (!headers) {
    return
  }
  Object.keys(headers).forEach(name => {
    if (name !== normalizedName && name.toUpperCase() === normalizedName.toUpperCase()) {
      headers[normalizedName] = headers[name]
      delete headers[name]
    }
  })
}

export function processHeaders (headers: any, data: any): any {
  normalizeHeaderName(headers, 'Content-Type')

  if (isPlainObject(data)) {
    if (headers && !headers['Content-Type']) {
      headers['Content-Type'] = 'application/json;charset=utf-8'
    }
  }
  return headers
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

这里有个需要注意的点,因为请求 header 属性是大小写不敏感的,比如我们之前的例子传入 header 的属性名 content-type 就是全小写的,所以我们先要把一些 header 属性名规范化。

# 实现请求 header 处理逻辑

在这之前,我们先修改一下 AxiosRequestConfig 接口类型的定义,添加 headers 这个可选属性:

types/index.ts

export interface AxiosRequestConfig {
  url: string
  method?: Method
  data?: any
  params?: any
  headers?: any
}
1
2
3
4
5
6
7

index.ts:

function processConfig (config: AxiosRequestConfig): void {
  config.url = transformURL(config)
  config.headers = transformHeaders(config)
  config.data = transformRequestData(config)
}

function transformHeaders (config: AxiosRequestConfig) {
  const { headers = {}, data } = config
  return processHeaders(headers, data)
}
1
2
3
4
5
6
7
8
9
10

因为我们处理 header 的时候依赖了 data,所以要在处理请求 body 数据之前处理请求 header。

xhr.ts:

export default function xhr (config: AxiosRequestConfig): void {
  const { data = null, url, method = 'get', headers } = config

  const request = new XMLHttpRequest()

  request.open(method.toUpperCase(), url, true)

  Object.keys(headers).forEach((name) => {
    if (data === null && name.toLowerCase() === 'content-type') {
      delete headers[name]
    } else {
      request.setRequestHeader(name, headers[name])
    }
  })

  request.send(data)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

这里要额外判断一个逻辑,当我们传入的 data 为空的时候,请求 header 配置 Content-Type 是没有意义的,于是我们把它删除。

# demo 编写

axios({
  method: 'post',
  url: '/base/post',
  data: {
    a: 1,
    b: 2
  }
})

axios({
  method: 'post',
  url: '/base/post',
  headers: {
    'content-type': 'application/json;'
  },
  data: {
    a: 1,
    b: 2
  }
})

const paramsString = 'q=URLUtils.searchParams&topic=api'
const searchParams = new URLSearchParams(paramsString)

axios({
  method: 'post',
  url: '/base/post',
  data: searchParams
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

通过 demo 我们可以看到,当我们请求的数据是普通对象并且没有配置 headers 的时候,会自动为其添加 Content-Type:application/json;charset=utf-8;同时我们发现当 data 是某些类型如 URLSearchParams 的时候,浏览器会自动为请求 header加上合适的 Content-Type。

至此我们对于请求的处理逻辑暂时告一段落。目前我们的请求从网络层面是可以收到服务端的响应的,下一节课我们就从代码层面来处理服务端响应,并且让调用方可以拿到从服务端返回的数据。

#TypeScript
上次更新: 2023/08/25, 16:22:35
处理请求 body 数据
获取响应数据

← 处理请求 body 数据 获取响应数据→

最近更新
01
我做了一个手写春联小网页,祝大家虎年暴富 原创
01-28
02
一行代码“黑”掉任意网站 原创
11-25
03
33个非常实用的JavaScript一行代码
11-02
更多文章>
Theme by Vdoing | Copyright © 2023-2025 左本
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式