curl / Mailing Lists / curl-users / Single Mail
Buy commercial curl support from WolfSSL. We help you work out your issues, debug your libcurl applications, use the API, port to new platforms, add new features and more. With a team lead by the curl founder himself.

Re: Failed to use MathpixOCR JSON API with curl.

From: Hongyi Zhao via curl-users <curl-users_at_cool.haxx.se>
Date: Wed, 9 Jun 2021 22:01:02 +0800

On Wed, Jun 9, 2021 at 5:52 PM Jeremy Nicoll via curl-users
<curl-users_at_cool.haxx.se> wrote:
>
> On Wed, 9 Jun 2021, at 04:15, Hongyi Zhao via curl-users wrote:
> > According to the instruction given on the MathpixOCR API official
> > website here, <https://docs.mathpix.com/#request-parameters>, as shown
> > below:
> >
> > curl -X POST https://api.mathpix.com/v3/text \
> > -H 'app_id: YOUR_APP_ID' \
> > -H 'app_key: YOUR_APP_KEY' \
> > -H 'Content-Type: application/json' \
> > --data '{ "src": "data:image/jpeg;base64,'$(base64 -i limit.jpg)'" }'
> >
> > I use the following code with my own app_id and app_key to test the
> > OCR accuracy of MathpixOCR:
> >
> > $ curl -x socks5://127.0.0.1:18888 -X POST
> > https://api.mathpix.com/v3/latex -H 'app_id: my_app_id' -H
> > 'app_key: my_app_key' -H 'Content-Type: application/json'
> > --data '{ "url": "data:image/jpg;base64, '$(base64 -i
> > schrodinger.png)'" }'
> > {"error":"SyntaxError: Unexpected end of JSON
>
> Does it matter that despite the webpage saying that the --data value
> should start with "src", you used "url"?

Both of them will work. See following.

>
> Have you tried checking the syntax of your JSON parameters, either
> at an online JSON unscrambling/formatting page, or in your own code?
>
>
> > From my point of view, the test example
> > of mine is exatly same with the official sample code.
>
> It clearly is not. But I have no idea if the difference matters.

Thank you for your comments. Based on some further tries, I find that
the following shell variable substitute method for the HTTP POST data
block will work:

$ cat test.sh
echo === src ===

curl -x socks5://127.0.0.1:18888 -X POST
https://api.mathpix.com/v3/text -H 'app_id: my_app_id' -H 'app_key:
my_app_key' -H 'Content-Type: application/json' --data "$(echo '{
"src": "data:image/jpeg;base64,'$(base64 -i schrodinger.png
2>/dev/null)'" }' )"

echo

echo === url ===
curl -x socks5://127.0.0.1:18888 -X POST
https://api.mathpix.com/v3/text -H 'app_id: my_app_id' -H 'app_key:
my_app_key' -H 'Content-Type: application/json' --data "$(echo '{
"url": "data:image/jpeg;base64,'$(base64 -i schrodinger.png
2>/dev/null)'" }' )"

echo

$ bash test.sh
=== src ===
{"request_id":"c19708139a07604fd010b084a614b82a","is_printed":true,"is_handwritten":false,"auto_rotate_confidence":0.00018956642596990037,"auto_rotate_degrees":0,"confidence":1,"confidence_rate":1,"latex_styled":"i
\\hbar \\frac{\\partial}{\\partial t} \\Psi(x,
t)=\\left[-\\frac{\\hbar^{2}}{2 m} \\frac{\\partial^{2}}{\\partial
x^{2}}+V(x, t)\\right] \\Psi(x, t)","text":"\\( i \\hbar
\\frac{\\partial}{\\partial t} \\Psi(x,
t)=\\left[-\\frac{\\hbar^{2}}{2 m} \\frac{\\partial^{2}}{\\partial
x^{2}}+V(x, t)\\right] \\Psi(x, t) \\)"}
=== url ===
{"request_id":"16816daf8205f68b58ff17a938cff56b","is_printed":true,"is_handwritten":false,"auto_rotate_confidence":0.00018956642596990037,"auto_rotate_degrees":0,"confidence":1,"confidence_rate":1,"latex_styled":"i
\\hbar \\frac{\\partial}{\\partial t} \\Psi(x,
t)=\\left[-\\frac{\\hbar^{2}}{2 m} \\frac{\\partial^{2}}{\\partial
x^{2}}+V(x, t)\\right] \\Psi(x, t)","text":"\\( i \\hbar
\\frac{\\partial}{\\partial t} \\Psi(x,
t)=\\left[-\\frac{\\hbar^{2}}{2 m} \\frac{\\partial^{2}}{\\partial
x^{2}}+V(x, t)\\right] \\Psi(x, t) \\)"}

But I still can't figure out why the HTTP POST data block must be
passed this way.

Regards
-- 
Assoc. Prof. Hongyi Zhao <hongyi.zhao_at_gmail.com>
Theory and Simulation of Materials
Hebei Vocational University of Technology and Engineering
NO. 552 North Gangtie Road, Xingtai, China
-----------------------------------------------------------
Unsubscribe: https://cool.haxx.se/list/listinfo/curl-users
Etiquette:   https://curl.haxx.se/mail/etiquette.html
Received on 2021-06-09