---
name: "UploadTask.onHeadersReceived"
mode: "kit"
versionRequirements:
  - { name: "BaseKit", version: "2.0.1" }
platform:
  - "iOS"
  - "Android"
title: "UploadTask.onHeadersReceived - 监听 HTTP Response Header 事件。会比请求完成事件更早"
---

## UploadTask.onHeadersReceived

> [VERSION] BaseKit >= 2.0.1

> [PLATFORM] iOS, Android

### 描述

监听 HTTP Response Header 事件。会比请求完成事件更早

### 参数

`(listener: Function)`

| 参数 | 类型 | 必填 | 默认值 | 描述 |
| --- | --- | --- | --- | --- |
| `listener` | `(params: Object) => void` | 是 | - | 事件监听回调函数 |

##### UploadTask.onHeadersReceived.listener(params) 的属性

| 属性 | 类型 | 必填 | 默认值 | 最低版本 | 描述 |
| --- | --- | --- | --- | --- | --- |
| `header` | `Record<string, string>` | 是 | - | `2.0.1` | 开发者服务器返回的 HTTP Response Header |
| `requestId` | `string` | 是 | - | `2.0.1` | 网络请求id |


### 示例代码

#### Demo

```tsx
import { downloadFile, uploadFile } from '@ray-js/ray'

// 监听响应头：拿到 UploadTask 后注册 onHeadersReceived，收到服务器响应头时回调（比完成更早）
downloadFile({
  url: "https://airtake-public-data-1254153901.cos.ap-shanghai.myqcloud.com/ttttestfile/test_image.jpg",
  success: ({ tempFilePath }) => {
    const task = uploadFile({
      url: "https://httpbin.org/post",
      filePath: tempFilePath,
      name: "file",
      success: () => console.log("上传完成"),
      fail: error => console.error(error),
    });
    task.onHeadersReceived(e => {
      console.log("收到响应头：", JSON.stringify(e.header));
    });
  },
  fail: error => console.error(error),
});
```
