【问题标题】:Angular & DocRaptor - How to make a HTTP post and get the download URL as a responseAngular & DocRaptor - 如何发布 HTTP 帖子并获取下载 URL 作为响应
【发布时间】:2018-03-21 08:23:42
【问题描述】:

我正在尝试将 Angular5 与 DocRaptor 一起使用,但遇到了一些问题。他们的网站上没有任何关于如何与 Angular 集成的文档,但是有一个“other”的入门指南。我也调查了这个问题here (/using-docraptor-web-service-with-angularjs),但运气不佳。

我的模块

在我的模块中,我添加了以下内容:

import { HttpClientModule } from '@angular/common/http';

我的组件

在我已导入的组件中:

import { HttpClient } from '@angular/common/http';

我有一个按钮,它接受一个字符串并生成一个 PDF。我对发布请求不太了解。以下是我尝试过的:

return this.http.post('https://myAPIKEY@docraptor.com/docs', {
  "test": true,
  "name": "testDocument.pdf",
  "document_content": '<div>PDF generation test</div>',
  "type": "pdf",
}).subscribe(
  res => {
    console.log(res);
  },
  err => {
    console.log("Error occured");
  }
);

这会导致两个错误:

POST https://docraptor.com/docs401(未经授权)

无法加载https://docraptor.com/docs:请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,Origin 'https://mydomain' 不允许访问。响应的 HTTP 状态代码为 401。

对于如何正确地将内容发布到 DocRaptor 并在没有 CORS 问题的情况下接收下载 URL 的任何帮助,我们将不胜感激。

更新

我设法让以下代码成功发布到 DocRaptor。我只需要使用返回的状态 ID 发出请求:

return this.http.post('https://docraptor.com/docs?user_credentials=mycreds', forDocRaptor).subscribe(res => {
  console.log(res);
},
  err => {
    console.log("Error occured = ", err);
  }
)

【问题讨论】:

  • 根据他们的网站:“此外,DocRaptor 完全支持 CORS 规范,允许跨站点 HTTP 请求” - 我相信我的代码有问题。我只是不确定那是什么。一旦代码正确,我认为 cors 应该不是问题。
  • DocRaptor 开发者在这里:如果您使用异步文档,则只需要发出状态请求,仅当您的文档生成时间超过 60 秒时才需要。我假设大多数 Angular 文档不会花费那么长时间,您可能只使用同步文档生成。这可能会让事情变得简单一些。

标签: angular http http-post


【解决方案1】:

根据https://docraptor.com/documentation/node,您应该在正文中发布您的凭据

return this.http.post('https://docraptor.com/docs', 
{
user_credentials: "YOUR_API_KEY_HERE",
doc: {
  "test": true,
  "name": "testDocument.pdf",
  "document_content": '<div>PDF generation test</div>',
  "type": "pdf",
}
}
).subscribe(
  res => {
    console.log(res);
  },
  err => {
    console.log("Error occured");
  }
);

【讨论】:

  • 谢谢。是的,这产生了与我帖子中的更新相同的结果。现在唯一的问题是我需要针对返回的状态 ID docraptor.com/status{status_id} 发出请求。
猜你喜欢
  • 1970-01-01
  • 2011-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-10
  • 1970-01-01
  • 2020-05-14
相关资源
最近更新 更多