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
      • Requirements Analysis
        • Array Parameter Values
        • Object Parameter Values
        • Date Parameter Values
        • Special Character Support
        • Ignoring Null Values
        • Discarding URL Hash Marks
        • Preserving Existing URL Parameters
      • Implementing the buildURL Function
      • Implementing URL Parameter Processing Logic
      • Writing the Demo
    • Handling Request Body Data
    • Handling Request Headers
    • Retrieving Response Data
    • Handling Response Headers
    • Handling Response Data
  • ts-axios 异常情况处理

  • ts-axios 接口扩展

  • ts-axios 拦截器实现

  • ts-axios 配置化实现

  • ts-axios 取消功能实现

  • ts-axios 更多功能实现

  • ts-axios 单元测试

  • ts-axios 部署与发布

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

Handling Request URL Parameters

# Handling Request URL Parameters

# Requirements Analysis

Remember the problem we left from the previous lesson. Let's look at this example again:

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

We want the final request URL to be /base/get?a=1&b=2, so the server can parse the parameter data from the request URL. Essentially, we need to append the params object's keys and values to the url.

Let's look at some more complex examples.

# Array Parameter Values

axios({
  method: 'get',
  url: '/base/get',
  params: {
    foo: ['bar', 'baz']
  }
})
1
2
3
4
5
6
7

The final request URL should be /base/get?foo[]=bar&foo[]=baz'.

# Object Parameter Values

axios({
  method: 'get',
  url: '/base/get',
  params: {
    foo: {
      bar: 'baz'
    }
  }
})
1
2
3
4
5
6
7
8
9

The final request URL should be /base/get?foo=%7B%22bar%22:%22baz%22%7D, where the value after foo is the encoded result of {"bar":"baz"}.

# Date Parameter Values

const date = new Date()

axios({
  method: 'get',
  url: '/base/get',
  params: {
    date
  }
})
1
2
3
4
5
6
7
8
9

The final request URL should be /base/get?date=2019-04-01T05:55:39.030Z, where the value after date is the result of date.toISOString().

# Special Character Support

For characters @, :, $, ,, , [, ], we allow them to appear in the URL and don't want them to be encoded.

axios({
  method: 'get',
  url: '/base/get',
  params: {
    foo: '@:$, '
  }
})
1
2
3
4
5
6
7

The final request URL should be /base/get?foo=@:$+. Note that we convert spaces to +.

# Ignoring Null Values

For properties with null or undefined values, we don't add them to the URL parameters.

axios({
  method: 'get',
  url: '/base/get',
  params: {
    foo: 'bar',
    baz: null
  }
})
1
2
3
4
5
6
7
8

The final request URL should be /base/get?foo=bar.

# Discarding URL Hash Marks

axios({
  method: 'get',
  url: '/base/get#hash',
  params: {
    foo: 'bar'
  }
})
1
2
3
4
5
6
7

The final request URL should be /base/get?foo=bar.

# Preserving Existing URL Parameters

axios({
  method: 'get',
  url: '/base/get?foo=bar',
  params: {
    bar: 'baz'
  }
})
1
2
3
4
5
6
7

The final request URL should be /base/get?foo=bar&bar=baz.

# Implementing the buildURL Function

Based on our requirements analysis, we need to implement a utility function that appends params to the url. We want to manage utility functions and helper methods independently, so we create a helpers directory and create a url.ts file inside it for URL-related utility functions.

helpers/url.ts:

import { isDate, isObject } from './util'

function encode (val: string): string {
  return encodeURIComponent(val)
    .replace(/%40/g, '@')
    .replace(/%3A/gi, ':')
    .replace(/%24/g, '$')
    .replace(/%2C/gi, ',')
    .replace(/%20/g, '+')
    .replace(/%5B/gi, '[')
    .replace(/%5D/gi, ']')
}

export function bulidURL (url: string, params?: any) {
  if (!params) {
    return url
  }

  const parts: string[] = []

  Object.keys(params).forEach((key) => {
    let val = params[key]
    if (val === null || typeof val === 'undefined') {
      return
    }
    let values: string[]
    if (Array.isArray(val)) {
      values = val
      key += '[]'
    } else {
      values = [val]
    }
    values.forEach((val) => {
      if (isDate(val)) {
        val = val.toISOString()
      } else if (isObject(val)) {
        val = JSON.stringify(val)
      }
      parts.push(`${encode(key)}=${encode(val)}`)
    })
  })

  let serializedParams = parts.join('&')

  if (serializedParams) {
    const markIndex = url.indexOf('#')
    if (markIndex !== -1) {
      url = url.slice(0, markIndex)
    }

    url += (url.indexOf('?') === -1 ? '?' : '&') + serializedParams
  }

  return url
}
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

helpers/util.ts:

const toString = Object.prototype.toString

export function isDate (val: any): val is Date {
  return toString.call(val) === '[object Date]'
}

export function isObject (val: any): val is Object {
  return val !== null && typeof val === 'object'
}

1
2
3
4
5
6
7
8
9
10

# Implementing URL Parameter Processing Logic

We've implemented the buildURL function. Now let's use it to implement the URL parameter processing logic.

Add the following code in index.ts:

function axios (config: AxiosRequestConfig): void {
  processConfig(config)
  xhr(config)
}

function processConfig (config: AxiosRequestConfig): void {
  config.url = transformUrl(config)
}

function transformUrl (config: AxiosRequestConfig): string {
  const { url, params } = config
  return bulidURL(url, params)
}
1
2
3
4
5
6
7
8
9
10
11
12
13

Before executing the xhr function, we first execute the processConfig method to process the data in config. Besides handling url and params, we'll process other properties in the future.

Inside processConfig, we modify config.url by executing the transformUrl function, which internally calls buildURL.

With that, the URL parameter processing logic is complete. Next, let's write a demo.

# Writing the Demo

Create a base directory under examples, and create index.html inside:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Base example</title>
  </head>
  <body>
    <script src="/__build__/base.js"></script>
  </body>
</html>
1
2
3
4
5
6
7
8
9
10

Then create app.ts as the entry file:

import axios from '../../src/index'

axios({
  method: 'get',
  url: '/base/get',
  params: {
    foo: ['bar', 'baz']
  }
})

axios({
  method: 'get',
  url: '/base/get',
  params: {
    foo: {
      bar: 'baz'
    }
  }
})

const date = new Date()

axios({
  method: 'get',
  url: '/base/get',
  params: {
    date
  }
})

axios({
  method: 'get',
  url: '/base/get',
  params: {
    foo: '@:$, '
  }
})

axios({
  method: 'get',
  url: '/base/get',
  params: {
    foo: 'bar',
    baz: null
  }
})

axios({
  method: 'get',
  url: '/base/get#hash',
  params: {
    foo: 'bar'
  }
})

axios({
  method: 'get',
  url: '/base/get?foo=bar',
  params: {
    bar: 'baz'
  }
})
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

Then add a new route in server.js:

router.get('/base/get', function(req, res) {
  res.json(req.query)
})
1
2
3

Run npm run dev in the terminal, then open Chrome and visit http://localhost:8080/ to access our demo. Click into the Base directory, and through the network tab in Developer Tools, you can see multiple requests were sent successfully, and their final URLs have the request parameters properly appended.

With that, we've finished implementing request URL parameter handling. In the next section, we'll handle request body data.

Edit (opens new window)
#TypeScript
Last Updated: 2026/03/21, 12:14:36
Writing Basic Request Code
Handling Request Body Data

← Writing Basic Request Code Handling Request Body 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