将 http 选项设置为 'responseType: 'text'' 会导致使用 Angular HttpClient 的 http post 请求编译失败

2023-12-24

虽然 Angular HttpClient 中有一个重载的 API 方法,如下所示:

 post(url: string, body: any | null, options: {
        headers?: HttpHeaders | {
            [header: string]: string | string[];
        };
        observe?: 'body';
        params?: HttpParams | {
            [param: string]: string | string[];
        };
        reportProgress?: boolean;
        responseType: 'text';
        withCredentials?: boolean;
    }): Observable<string>;

当我尝试使用选项 'responseType: 'text'' 执行以下发布请求时,出现编译器错误,指出“没有重载与此调用匹配。...”

const opts = {
      headers: new HttpHeaders({
        'Accept': 'text/html, application/xhtml+xml, */*',
        'Content-Type': 'text/plain; charset=utf-8'
      }),
      responseType: 'text'
    };
    return this.http.post<string>('url', null, opts);

注意:我明智地传递 null 作为响应主体,因为我的 API 不接受主体。 我在这里做的可能是错的吗?

Update根据 Michael D 的要求添加完整的错误消息

Type 'Observable<ArrayBuffer>' is not assignable to type 'Observable<string>'.
  Type 'ArrayBuffer' is not assignable to type 'string'.ts(2322)
No overload matches this call.
  Overload 1 of 15, '(url: string, body: any, options: { headers?: HttpHeaders | { [header: string]: string | string[]; }; observe: "events"; params?: HttpParams | { [param: string]: string | string[]; }; reportProgress?: boolean; responseType?: "json"; withCredentials?: boolean; }): Observable<...>', gave the following error.
    Argument of type '{ headers: HttpHeaders; responseType: string; }' is not assignable to parameter of type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe: "events"; params?: HttpParams | { [param: string]: string | string[]; }; reportProgress?: boolean; responseType?: "json"; withCredentials?: boolean; }'.
      Property 'observe' is missing in type '{ headers: HttpHeaders; responseType: string; }' but required in type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe: "events"; params?: HttpParams | { [param: string]: string | string[]; }; reportProgress?: boolean; responseType?: "json"; withCredentials?: boolean; }'.
  Overload 2 of 15, '(url: string, body: any, options: { headers?: HttpHeaders | { [header: string]: string | string[]; }; observe: "response"; params?: HttpParams | { [param: string]: string | string[]; }; reportProgress?: boolean; responseType?: "json"; withCredentials?: boolean; }): Observable<...>', gave the following error.
    Argument of type '{ headers: HttpHeaders; responseType: string; }' is not assignable to parameter of type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe: "response"; params?: HttpParams | { [param: string]: string | string[]; }; reportProgress?: boolean; responseType?: "json"; withCredentials?: boolean; }'.
      Property 'observe' is missing in type '{ headers: HttpHeaders; responseType: string; }' but required in type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe: "response"; params?: HttpParams | { [param: string]: string | string[]; }; reportProgress?: boolean; responseType?: "json"; withCredentials?: boolean; }'.
  Overload 3 of 15, '(url: string, body: any, options?: { headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: HttpParams | { [param: string]: string | string[]; }; reportProgress?: boolean; responseType?: "json"; withCredentials?: boolean; }): Observable<...>', gave the following error.
    Argument of type '{ headers: HttpHeaders; responseType: string; }' is not assignable to parameter of type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: HttpParams | { [param: string]: string | string[]; }; reportProgress?: boolean; responseType?: "json"; withCredentials?: boolean; }'.
      Types of property 'responseType' are incompatible.
        Type 'string' is not assignable to type '"json"'.ts(2769)
http.d.ts(2293, 9): 'observe' is declared here.
http.d.ts(2409, 9): 'observe' is declared here.

问题在于responseType财产。像这样将其分配为字符串不会立即起作用。

您可以将其转换为text type

const opts  = {
  headers: new HttpHeaders({
    'Accept': 'text/html, application/xhtml+xml, */*',
    'Content-Type': 'text/plain; charset=utf-8'
  }),
  responseType: 'text' as 'text' //<== here
};

    this.http.post(....);

Edit这仅适用于使用非通用版本的 http 调用(this.http.post(...)但不是通用版本this.http.post<string>)

或者,您可以将您的选项声明为any,这应该可以阻止编译器抱怨

const opts : any = {
  headers: new HttpHeaders({
    'Accept': 'text/html, application/xhtml+xml, */*',
    'Content-Type': 'text/plain; charset=utf-8'
  }),
  responseType: 'text'
};

this.http.post<string>(....)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将 http 选项设置为 'responseType: 'text'' 会导致使用 Angular HttpClient 的 http post 请求编译失败 的相关文章

随机推荐

  • 更改默认的 Ruby 参数

    我想更改传递给 Ruby 函数的默认参数 例如 而不是每次都写 1 2 3 do stuff option gt my option 我想修改默认值以便我可以编写 1 2 3 do stuff 更改默认参数的最简单 最干净 最像 Ruby
  • 通过 ssh 密钥访问 GitHub 组织

    也许我是瞎子 但我找不到一种方法来访问通过 SSH 密钥授予我访问权限的组织 我知道直接连接到您自己的帐户很容易 但是我该如何通过组织来做到这一点呢 您帐户的个人 SSH 密钥可识别您的 GitHub 用户 如果组织授予您访问权限 则这是通
  • 行进立方体问题

    我目前正在编写一个程序来使用 C 和 Opengl 实现 Marching Cube 然而 我最好的参考仅来自http local wasp uwa edu au pbourke geometry polygonise http local
  • 在 Rcpp 中的另一个 cpp 文件中定义的函数中使用一个 cpp 文件中定义的函数

    我有一个名为的 C 函数add文件中定义add cpp 内容add cpp below include
  • Mysql暂时抑制唯一索引

    我有一个在两列上有唯一索引的表 准确地说是 id parent 和 sort order id id parent sort order some data other data more data 1 1 1 lorem ipsum lo
  • 如何在 VS Code for Markdown 中禁用 IntelliSense?

    我不想在 Visual Studio Code 中为 Markdown 文件提供单词补全功能 如何禁用它 理想情况下 仅适用于 Markdown 但在最坏的情况下 即使是全局切换也是好的 可以配置 VS Code 中的 IntelliSen
  • SQL Server 2008 R2 的百分位数聚合

    我正在使用 SQL Server 2008 R2 我需要计算每个组的百分位数 例如 SELECT id PCTL 0 9 x for the 90th percentile FROM my table GROUP BY id ORDER B
  • PhpStorm 显示/隐藏主菜单

    我错误地使用 视图 gt 外观 隐藏了 PhpStorm 的主菜单 未选中菜单 我尝试了很多关于如何再次显示主菜单的搜索 不久之后我找到了一个非常简单的方法 Press Double Shift用于到处搜索 现在搜索任何设置或您想要更改的任
  • AudioKit AKPlayer 无法回环到 setPosition 开头

    在 v4 中 如果您使用AKPlayer循环并使用setPositionAPI 玩家在设置的位置之间循环setPosition以及玩家的总持续时间 我需要的是从设置的位置开始setPositionAPI 并在循环发生时循环到开头 我不确定这
  • Mongoose 填充子文档数组

    抱歉 如果已经有人问过这个问题 我的搜索没有出现同样的情况 我有两个架构 如下所示 var experimentSchema new mongoose Schema name string elements type mongoose Sc
  • 使用 Firebase 通过聊天应用进行语音通话

    我正在尝试构建一个简单的聊天应用程序 但它需要与其他用户进行语音通话的功能 我将使用 Firebase 进行消息传递 Firebase 是否支持语音通话 我可以配置外部 SIP 如果你想创建语音通话或视频通话应用程序 也许你可以使用WebR
  • 有没有一种方法可以将复杂度为 O(n) 的矩阵相乘?

    I want to multiply two matrices but the triple loop has O n3 complexity Is there any algorithm in dynamic programming to
  • 为什么使用函数调用而不是变量地址来检测堆栈增长方向?

    我读到了对检测堆栈增长检测问题的不同回答 我了解到 在现代架构中 堆栈可能会随机增长 可能会在堆外创建 等等 然而 在这个经典的面试问题中 我想明白why人们使用函数调用而不是比较同一函数中的两个局部变量 我认为这样做一定有一些特殊的原因
  • 如何在c中一次从一串没有空格的字符中取出一个字符作为输入?

    假设 5181 2710 9900 0012 是一串数字 我需要一次从没有空格的数字串中取出一个数字作为输入来进行算术运算 所以 我写的是 int a 20 for int i 0 i lt 16 i scanf d a i 但它没有给我预
  • __STDC_LIB_EXT1__ 在 gcc 和 clang 中的可用性

    由于快速谷歌搜索没有找到任何东西 我会尝试在这里询问 因为很多参与 gcc clang 的人都在这里闲逛 的状态是什么 STDC LIB EXT1 在海湾合作委员会 铿锵 我们正在开发一个跨平台应用程序 我想使用一些安全边界检查功能
  • 如何在 PHP 中解决这个问题?

    onethird 1 0 3 fivethirds 1 0 3 1 0 3 1 0 3 1 0 3 1 0 3 half 1 0 2 threehalf 1 0 2 1 0 2 1 0 2 var dump onethird fivethi
  • 角度 - 单击时更新指令模板

    我正在尝试使用 Angular 构建一个页面 我有两个模板 比如说 and 我想创建一个指令 当单击模板内的按钮时 将其模板从 a 更改为 b 反之亦然 我尝试了几种选择 查看类似问题的其他答案 但没有任何运气 你
  • 使用多个时间戳服务器进行代码签名

    关于时间戳和使用 Signtool 进行代码签名 将多个时间戳机构的时间戳应用于签名代码是否被认为是最佳实践 可能有助于我理解的其他相关问题 是否可以利用不同的时间戳服务器应用多个时间戳 如果我对这篇文章的理解是正确的 那么答案是肯定的 看
  • 何时使用 GWT EnsureInjected()?

    我在 CSSResource 中创建了一些样式 无论我使用什么 它都可以很好地工作 GWT
  • 将 http 选项设置为 'responseType: 'text'' 会导致使用 Angular HttpClient 的 http post 请求编译失败

    虽然 Angular HttpClient 中有一个重载的 API 方法 如下所示 post url string body any null options headers HttpHeaders header string string