Sending http post to ocr space api

北战南征 提交于 2020-01-06 07:13:31

问题


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

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!


回答1:


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))


来源:https://stackoverflow.com/questions/47484744/sending-http-post-to-ocr-space-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!