【问题标题】:Can't convert successful Postman call to Ionic-Native HTTP.post [Ionic 2 ts]无法将成功的 Postman 调用转换为 Ionic-Native HTTP.post [Ionic 2 ts]
【发布时间】:2017-02-18 10:42:20
【问题描述】:

我正在构建一个 Ionic 2 (ts) 应用程序,它将向 OCR.space API 发送一个 REST 调用。通过他们的示例,我能够通过 HTTP.post 发送 Base64Image,但是当尝试通过 HTTP.Post 发送文件时,我遇到了:

{"ParsedResults":null,"OCRExitCode":0,"IsErroredOnProcessing":false,"ErrorMessage":["Parameter
        name 'file' is invalid. Valid parameters:
        apikey,url,language,isoverlayrequired,base64image"],"ErrorDetails":null,"ProcessingTimeInMilliseconds":"1"}

我猜这是我发帖请求的格式:

        HTTP.post('http://api.ocr.space/parse/image',
    { "apikey":"helloworld", "language":"eng", "isOverlayRequired":"false", "file": "asssets/img/test2.pdf" }, {})
      .then(data => {
        console.log("HTTP entered");
        let result = JSON.parse(data.data); // data received by server
        console.log(data.data);
      })
      .catch(error => {
        console.log(error.error); // error message as string
      });

我猜这是因为我能够通过邮递员成功发送 PDF 文件,如下所示:my successful postman request

所以 - 我很想帮助我弄清楚如何成功发送这个 HTTP.post 请求,或者将我可以从邮递员那里获得的代码转换为成功的 ionic-native 语法。

    var form = new FormData();
form.append("apikey", "541496f13e88957");
form.append("language", "eng");
form.append("isOverlayRequired", "false");
form.append("file", "1page.pdf");

var settings = {
  "async": true,
  "crossDomain": true,
  "url": "https://api.ocr.space/parse/image",
  "method": "POST",
  "headers": {
    "cache-control": "no-cache",
    "postman-token": "1aea47d5-a0eb-7768-5fa6-60c4cd76d453"
  },
  "processData": false,
  "contentType": false,
  "mimeType": "multipart/form-data",
  "data": form
}

$.ajax(settings).done(function (response) {
  console.log(response);
});

感谢所有帮助!

【问题讨论】:

  • 您收到的错误信息是什么?它在哪里不起作用?使用ionic serve 测试时在浏览器中还是在设备上?
  • 错误消息位于 json 内的第一个代码块中 - API 调用正在工作 - 我得到了响应 - 但我认为我的响应格式已关闭,API 不理解我的要求。 "ErrorMessage":["参数名 'file' 无效。有效参数:apikey,url,language,isoverlayrequired,base64image" 但是文档明确指出文件是有效参数。

标签: http ionic-framework ionic2 ionic-native


【解决方案1】:

我使用 cordova-plugin-file-transfer 发送文件而不是 Base64Image 或 Ionic 2 使用 ionic-native 和示例

【讨论】:

  • 我能再了解一点背景吗?
猜你喜欢
  • 2016-08-19
  • 2017-09-21
  • 1970-01-01
  • 2017-07-05
  • 2017-08-26
  • 1970-01-01
  • 2020-06-02
  • 1970-01-01
  • 2017-06-12
相关资源
最近更新 更多