VDone Demo VDone Demo
Home
  • Articles

    • JavaScript
  • Study Notes

    • JavaScript Tutorial
    • Professional JavaScript
    • ES6 Tutorial
    • Vue
    • React
    • TypeScript: Build Axios from Scratch
    • Git
    • TypeScript
    • JS Design Patterns
  • HTML
  • CSS
  • Technical Docs
  • GitHub Tips
  • Node.js
  • Blog Setup
  • Learning
  • Interviews
  • Miscellaneous
  • Practical Tips
  • Friends
About
Bookmarks
  • Categories
  • Tags
  • Archives
GitHub (opens new window)

Nikolay Tuzov

Backend Developer
Home
  • Articles

    • JavaScript
  • Study Notes

    • JavaScript Tutorial
    • Professional JavaScript
    • ES6 Tutorial
    • Vue
    • React
    • TypeScript: Build Axios from Scratch
    • Git
    • TypeScript
    • JS Design Patterns
  • HTML
  • CSS
  • Technical Docs
  • GitHub Tips
  • Node.js
  • Blog Setup
  • Learning
  • Interviews
  • Miscellaneous
  • Practical Tips
  • Friends
About
Bookmarks
  • Categories
  • Tags
  • Archives
GitHub (opens new window)
  • 初识 TypeScript

  • TypeScript 常用语法

  • ts-axios 项目初始化

  • ts-axios 基础功能实现

    • Handling Request URL Parameters
    • Handling Request Body Data
    • Handling Request Headers
    • Retrieving Response Data
    • Handling Response Headers
      • Requirements Analysis
      • Implementing and Applying the parseHeaders Function
    • Handling Response Data
  • ts-axios 异常情况处理

  • ts-axios 接口扩展

  • ts-axios 拦截器实现

  • ts-axios 配置化实现

  • ts-axios 取消功能实现

  • ts-axios 更多功能实现

  • ts-axios 单元测试

  • ts-axios 部署与发布

  • 《TypeScript 从零实现 axios》
  • ts-axios 基础功能实现
xugaoyi
2020-01-05
Contents

Handling Response Headers

# Handling Response Headers

# Requirements Analysis

The value we get from the XMLHttpRequest object's getAllResponseHeaders method is a string like the following:

date: Fri, 05 Apr 2019 12:40:49 GMT
etag: W/"d-Ssxx4FRxEutDLwo2+xkkxKc4y0k"
connection: keep-alive
x-powered-by: Express
content-length: 13
content-type: application/json; charset=utf-8
1
2
3
4
5
6

Each line ends with a carriage return and newline (\r\n), which serve as delimiters between header properties. For the string above, we want to parse it into an object structure:

{
  date: 'Fri, 05 Apr 2019 12:40:49 GMT'
  etag: 'W/"d-Ssxx4FRxEutDLwo2+xkkxKc4y0k"',
  connection: 'keep-alive',
  'x-powered-by': 'Express',
  'content-length': '13'
  'content-type': 'application/json; charset=utf-8'
}
1
2
3
4
5
6
7
8

# Implementing and Applying the parseHeaders Function

Based on the requirements analysis, we need to implement a parseHeaders utility function.

helpers/headers.ts:

export function parseHeaders(headers: string): any {
  let parsed = Object.create(null)
  if (!headers) {
    return parsed
  }

  headers.split('\r\n').forEach(line => {
    let [key, val] = line.split(':')
    key = key.trim().toLowerCase()
    if (!key) {
      return
    }
    if (val) {
      val = val.trim()
    }
    parsed[key] = val
  })

  return parsed
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

Then we use this utility function:

xhr.ts:

const responseHeaders = parseHeaders(request.getAllResponseHeaders())
1

Now if we revisit the earlier demo, we can see that the response headers field has been parsed from a string into an object structure. Next, let's solve the second outstanding issue: handling the response data field.

Edit (opens new window)
#TypeScript
Last Updated: 2026/03/21, 12:14:36
Retrieving Response Data
Handling Response Data

← Retrieving Response Data Handling Response Data→

Recent Updates
01
How I Discovered Disposable Email — A True Story
06-12
02
Animations in Grid Layout
09-15
03
Renaming a Git Branch
08-11
More Articles >
Theme by VDone | Copyright © 2026-2026 Nikolay Tuzov | MIT License | Telegram
  • Auto
  • Light Mode
  • Dark Mode
  • Reading Mode