Skip to content

Request Config ​

A request's behavior is described by RequestConfig (the request main entry) or RequestQuickConfig (shortcut methods, no url field). Apart from url, their fields map one-to-one; this chapter covers them one by one.

Field Overview ​

FieldTypeDescription
urlstring (required)Request path. Relative paths are joined with baseURL; urls starting with http:// / https:// are used as-is
methodRequestMethod | nullRequest method; omitted or null uses the instance default (GET). This field has no effect in shortcut methods (the method is fixed by the method name)
baseURLstring | nullRequest-level override of the instance default baseURL
paramsUTSJSONObject | nullQuery params, merged with the url's existing query and encoded uniformly in-library (see Params & Headers)
dataany | nullRequest body; for GET/HEAD a UTSJSONObject is automatically merged into the query
headerUTSJSONObject | nullRequest header, shallow-merged with the instance default header (request-level same-name keys override)
timeoutnumber | nullTimeout in milliseconds; omitted or null uses the instance default (60000)
dataTypestring | nullResponse dataType (json means the native layer JSON.parses), passed through natively
responseTypestring | nullResponse data type, passed through natively (ArrayBuffer scenarios: app-android 4.51+ / app-ios 4.61+)
validateStatus((statusCode: number) => boolean) | nullStatus code validation; omitted or null uses the instance default (2xx counts as success)
cancelTokenCancelToken | nullCancel token; omitted or null means not cancelable (see Cancellation)
transformRequest((data: any | null) => any) | nullRequest body transform; providing it replaces the built-in (see Transforming Requests & Responses)
transformResponse((data: any | null) => any) | nullResponse body transform; providing it replaces the built-in (the built-in is pass-through)
onTask((task: RequestTask) => void) | nullEscape hatch: get the native RequestTask (see below)
enableQuic / sslVerify / withCredentials / firstIpv4 / enableChunkedboolean | nullNative capability parameters, passed through fully without smoothing (see below)

Optional fields are deliberate

Every field except url is optional. This is a hard-won rule for UTS plugins: the plugin proxy layer generates non-null casts for required fields, so a missed field means a null-pointer crash; all-optional + in-library null checks guarantee the call site can always write just { url: '/x' }.

Nullable Semantics: Omitted or null Uses Instance Defaults ​

All optional fields share one consistent semantic: omitted or explicitly null means the instance default applies (falling back to the built-in defaults when the instance has none). Explicit null is equivalent to omitting, which keeps a uniform writing style in "dynamic configuration" scenarios:

uts
import { request } from '@/uni_modules/ux-request'

request<any>({
	url: '/api/list',
	method: null,      // uses the instance default (GET)
	timeout: null      // uses the instance default (60000 or the configureRequest value)
})

Three-Level Merging ​

The merge order is strictly: built-in defaults → instance defaults (configureRequest / createRequest / setConfig) → request-level RequestConfig.

ConfigBuilt-in defaultMerge rule
methodGETA request-level value overrides
timeout60000 msA request-level value overrides
validateStatus2xx passes (>= 200 && < 300)The callback overrides as a whole; no merging
baseURLnone (relative-path requests then fail-fast)Request level overrides the instance default
headernoneShallow merge: instance default keys are overridden by request-level same-name keys, other keys are kept
uts
import { request } from '@/uni_modules/ux-request'

// allow 3xx to count as success too (overrides the default 2xx validation)
request<any>({
	url: '/api/redirect-check',
	validateStatus: (statusCode: number): boolean => {
		return statusCode >= 200 && statusCode < 400
	}
})

For the full semantics of RequestDefaults and instance defaults see Instances & Global Defaults and the API doc RequestDefaults.

Native Pass-Through Parameters ​

The following fields are passed through to uni.request in full; this library does not smooth them over. Whether they take effect on each platform is subject to the official documentation:

  • enableQuic: QUIC transport
  • sslVerify: SSL certificate verification
  • withCredentials: cross-origin credentials
  • firstIpv4: prefer IPv4
  • enableChunked: chunked transfer (pair with onTask to listen for chunks)

Likewise, dataType / responseType are also passed through as-is. responseType: 'arraybuffer' has platform version differences (app-android 4.51+ / app-ios 4.61+); see Platform Compatibility for details.

onTask: The Native Task Escape Hatch ​

onTask is called back with the native RequestTask after the request is sent, for advanced scenarios this library does not wrap (chunked listening, response header listening, etc.):

uts
import { request } from '@/uni_modules/ux-request'

request<any>({
	url: '/api/stream',
	enableChunked: true,
	onTask: (task: RequestTask): void => {
		// native capabilities such as task.onChunkReceived / task.onHeadersReceived
	}
})

How manual aborts are classified

You can call abort() manually on the task obtained via onTask, but the resulting fail is classified as ERR_NATIVE (non-deterministic cancellation). Always cancel through the cancelToken channel — its semantics are 100% deterministic.

Where Config Ends Up: The Send Snapshot ​

After normalization, the library holds a "send snapshot": url fully joined, params encoded and cleared, header merged. response.config / error.config hold exactly this — inspect it directly when debugging "what the request actually looked like". See Request Flow for the underlying principles.

For the full type definitions see the API docs RequestConfig and RequestQuickConfig.

Released under the MIT License.