本文介绍了将http帖子发送到ocr space api的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Ionic应用程序发出HTTP请求到ocr.space API。

I’m trying to make an HTTP request from a Ionic app to to ocr.space API.

这是我写的代码,base64image来自于相机插件并且格式正确:

this is the code I wrote, the base64image comes from the Camera plugin and is correctly formatted:

let base64Image = 'data:image/jpeg;base64,' + imageData;

     let data = "base64Image=" + base64Image;

     this.http.post("https://api.ocr.space/parse/image",data,{
       headers: new HttpHeaders().set('Content-Type','application/x-www-form-urlencoded')
                                 .set('apikey',this.APIKEY),
     })
                .subscribe((res)=> console.log(res))

然而,我得到的回应是图像的格式是不正确(不正确)。我究竟做错了什么?谢谢你的帮助!

However the response I’m getting is that the format of the image is not correct (not true). What am I doing wrong? Thanks for the help!

推荐答案

我不知道我是否应该回答我自己的问题。解决方案非常简单,分析Nic提出的答案是更关注的关键。以下是对原始代码的简单编辑(刚刚在数据参数上添加了encodeURIComponent方法):现在它正常运行。

I don't know if I'm supposed to answer my own question. The solution was quite simple and analyzing the answer proposed by Nic with more attention was the key. Following is the simple edit of the original code(just added encodeURIComponent method on the data parameter): now it's working flawlessly.

let base64Image = 'data:image/jpeg;base64,' + imageData;
 let data = encodeURIComponent("base64Image")+"="+encodeURIComponent(base64Image);
 this.http.post("https://api.ocr.space/parse/image",data,{
   headers: new HttpHeaders().set('Content-Type','application/x-www-form-urlencoded')
                             .set('apikey',this.APIKEY),
 })
            .subscribe((res)=> console.log(res))

这篇关于将http帖子发送到ocr space api的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 11:36