首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Range7处理大型邮政http请求

Range7处理大型邮政http请求
EN

Stack Overflow用户
提问于 2019-09-04 08:36:03
回答 2查看 2.5K关注 0票数 2

我有post http请求,它发送大量数据,并包含可能相当大的数组。我试图拆分请求,所以我想发送一个包含所有数据的请求,我想将它分块发送。

代码语言:javascript
复制
// service.ts
saveRequest(request: AutomationChecklist) {
  return this.http
    .post(this.url + this.saveRequestUrl, request, this.options)
    .map((response: Response) => {
      return response.json();
    });
}

// automationChecklist.ts
export class AutomationChecklist {
  siteInformation: siteInformation;
  orderInformation: OrderInformation;
  requestInformation: RequestInformation;
  contactInformation: ContactInformation;
  installations: Installation[]; // save this separately, this one can be quite large
  completedAutomation: number;
  progress: number;
}

处理此请求的可能解决方案是什么?我读过关于forkjoin的文章,但不确定它是否适合这种情况?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-09-04 12:46:28

如果您想要分离installations,可以使用concatMapTo操作符来完成:

代码语言:javascript
复制
saveRequest(automations: AutomationChecklist, installations: Installation[]) {
  return this.http
    .post(this.url + this.saveRequestUrl, automations, this.options)
    .pipe(concatMapTo(this.http.post(this.url + /*installations save url*/, installations, this.options)))
    .map((response: Response) => {
      return response.json();
    });
}

该解决方案的缺点:

  1. 针对相同需求的两个单独请求(创建一个新的后端端点)
  2. 非原子性:安装请求可能失败,但第一个请求成功(可能导致不确定的结果)
  3. 安装请求等待第一个终止

这取决于你想做什么,如果你接受不一致,它可能是一个很好的解决办法,以减轻你的要求。

票数 1
EN

Stack Overflow用户

发布于 2019-09-04 12:10:32

假设您有一个数据数组,下面的代码将以3的并发性发布到api。

代码语言:javascript
复制
array=[data,data,data]
const concurrency=3
from(array).pipe(mergeMap(request=>this.http
    .post(this.url + this.saveRequestUrl, request, this.options),null,concurrency)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57784571

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档