aa2zz6

HTTPS Post and Request

16 posts in this topic

#1 ·  Posted (edited)

Our operators are currently using app collector forms to store data. To increase productivity, I'm looking at using an online service that provides an api key for users, such as myself, to post app collector forms and be convert into text. This service would process the data much quicker than by typing each field by hand on ArcGIS Desktop 10.3.1, thus increasing productivity levels for our mapping department.

The goal is to use the available service to migrate the data into a more suitable format that would allow us to send our forms to be processed, returned and ready for our mapping.

I've included API key but I'm not familiar with implementing it. Currently, the website is requesting an API key, but I can't seem to implement it in the correct area for it to register. 

*The script doesn't include anywhere to send the files* I haven't gotten far enough or tested anything to include this.*

$oMyError = ObjEvent('AutoIt.Error", "MyErrFunc') ; Install a custom error handler

$link = 'https://api.ocr.space/parse/image'
$api_key ='8f1e0a750088957' ;~ API Key ~;
$oHTTP = ObjCreate('winhttp.winhttprequest.5.1')
$oHTTP.Open ('POST', $link)
$oHTTP.Send()
If @error Then
    MsgBox(16, 'Error', '$oHTTP.Send() returned @error = ' & @error & ', and @extended = ' & @extended)
    Exit
Else
    $HTMLSource = $oHTTP.Responsetext
    MsgBox(64, 'Results', $HTMLSource)
EndIf

 

Edited by aa2zz6

Share this post


Link to post
Share on other sites



#2 ·  Posted

Hello here is a example using GET and POST methods.

 

ConsoleWrite(">GET METHOD USING IMAGE URL<" & @CRLF)
_GetMethodTest()
ConsoleWrite(">POST METHOD UPLOADING LOCAL IMAGE<" & @CRLF)
_PostMethodTest()


Func _PostMethodTest()
    Local Const $sAPIKey = '8f1e0a750088957'
    Local $sBoundary = "--------Boundary"
    Local $sHeaders = "Content-Type: multipart/form-data; boundary=" & $sBoundary & @CRLF
    Local $sData = ''
    Local $sFileName="OCR.png"
    Local $sFilePath=@ScriptDir & "\" & $sFileName
    Local $hFile=FileOpen($sFilePath,16);16=$FO_BINARY
    Local $sFileData=FileRead($hFile)
    FileClose($hFile)

    $sData &= "--" & $sBoundary & @CRLF
    $sData &= 'Content-Disposition: form-data; name="file"; filename="' & $sFileName  & '"' & @CRLF
    $sData &= 'Content-Type: application/octet-stream' & @CRLF & @CRLF
    $sData &= BinaryToString($sFileData,0) &  @CRLF
    $sData &= "--" & $sBoundary &  @CRLF
    $sData &= 'Content-Disposition: form-data; name="language"' & @CRLF & @CRLF
    $sData &="eng" & @CRLF
    $sData &=$sBoundary & "--"

    Local $oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
    $oHTTP.Open("POST", "https://api.ocr.space/Parse/Image", False)
    $oHTTP.SetRequestHeader("Content-Type", "multipart/form-data; " & "boundary=" & $sBoundary)
    $oHTTP.SetRequestHeader("apikey", $sAPIKey)
    $oHTTP.Send(StringToBinary($sData,1))
    Local $sReceived = $oHTTP.ResponseText
    ConsoleWrite($sReceived & @CRLF)

EndFunc   ;==>_PostMethodTest



Func _GetMethodTest()
    Local Const $sAPIKey = '8f1e0a750088957'
    Local $sReturn = InetRead('https://api.ocr.space/parse/imageurl?apikey=' & $sAPIKey & '&url=http://i.imgur.com/fwxooMv.png', 1) ;1=$INET_FORCERELOAD
    $sReturn = BinaryToString($sReturn)
    ConsoleWrite($sReturn & @CRLF)
EndFunc   ;==>_GetMethodTest

Consider to use WinHTTP UDF Instead winhttp COM object.

 

Saludos

1 person likes this

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

@Danyfirex Thanks for the reply. I'm tried out the POST method function and I got this error:

{"ParsedResults":[{"TextOverlay":{"Lines":[],"HasOverlay":false,"Message":"Text overlay is not provided as it is not requested"},"FileParseExitCode":1,"ParsedText":"","ErrorMessage":"","ErrorDetails":""}],"OCRExitCode":1,"IsErroredOnProcessing":false,"ErrorMessage":null,"ErrorDetails":null,"ProcessingTimeInMilliseconds":"1088"}

Do we need to set the  "isOverlayRequired=False"  within a Content-Disposition for it to register false with the image?

HasOverlay Overlay is present or not True/False depending upon if the overlay for the parsed result is present or not
$sData &= 'Content-Disposition: form-data; name ="isOverlayRequired"' & @CRLF & @CRLF
    $sData &= "False" & @CRLF

I tried including this but the error still occurs so I'm thinking the overlay isn't being applied correctly.

Edited by aa2zz6

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

mmm I see.

Edit: I think by default is  false. But You could do it this way. change true to false. :-P 

ConsoleWrite(">GET METHOD USING IMAGE URL<" & @CRLF)
_GetMethodTest()
ConsoleWrite(">POST METHOD UPLOADING LOCAL IMAGE<" & @CRLF)
_PostMethodTest()


Func _PostMethodTest()
    Local Const $sAPIKey = '8f1e0a750088957'
    Local $sBoundary = "--------Boundary"
    Local $sHeaders = "Content-Type: multipart/form-data; boundary=" & $sBoundary & @CRLF
    Local $sData = ''
    Local $sFileName = "OCR.png"
    Local $sFilePath = @ScriptDir & "\" & $sFileName
    Local $hFile = FileOpen($sFilePath, 16) ;16=$FO_BINARY
    Local $sFileData = FileRead($hFile)
    FileClose($hFile)

    $sData &= "--" & $sBoundary & @CRLF
    $sData &= 'Content-Disposition: form-data; name="file"; filename="' & $sFileName & '"' & @CRLF
    $sData &= 'Content-Type: application/octet-stream' & @CRLF & @CRLF
    $sData &= BinaryToString($sFileData, 0) & @CRLF
    $sData &= "--" & $sBoundary & @CRLF
    $sData &= 'Content-Disposition: form-data; name="language"' & @CRLF & @CRLF
    $sData &= "eng" & @CRLF
    $sData &= "--" & $sBoundary & @CRLF
    $sData &= 'Content-Disposition: form-data; name="isOverlayRequired"' & @CRLF & @CRLF
    $sData &= 'true' & @CRLF
    $sData &= "--" & $sBoundary & "--"  & @CRLF

    Local $oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
    $oHTTP.Open("POST", "https://api.ocr.space/Parse/Image", False)
    $oHTTP.SetRequestHeader("Content-Type", "multipart/form-data; " & "boundary=" & $sBoundary)
    $oHTTP.SetRequestHeader("apikey", $sAPIKey)
    $oHTTP.Send(StringToBinary($sData, 1))
    Local $sReceived = $oHTTP.ResponseText
    ConsoleWrite($sReceived & @CRLF)

EndFunc   ;==>_PostMethodTest



Func _GetMethodTest()
    Local Const $sAPIKey = '8f1e0a750088957'
    Local $sReturn = InetRead('https://api.ocr.space/parse/imageurl?apikey=' & $sAPIKey & '&isOverlayRequired=true&url=http://i.imgur.com/fwxooMv.png', 1) ;1=$INET_FORCERELOAD
    $sReturn = BinaryToString($sReturn)
    ConsoleWrite($sReturn & @CRLF)
EndFunc   ;==>_GetMethodTest

Saludos

Edited by Danyfirex
1 person likes this

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

@Danyfirex  - Thanks, it's working correctly.

Edited by aa2zz6

Share this post


Link to post
Share on other sites

Hello, I'm using your example but I do not understand those lines of code! and also gives me an error how good that if it works and between the error gets the information of the image

$sData &= "--" & $sBoundary & @CRLF
    $sData &= 'Content-Disposition: form-data; name="file"; filename="' & $sFileName & '"' & @CRLF
    $sData &= 'Content-Type: application/octet-stream' & @CRLF & @CRLF
    $sData &= BinaryToString($sFileData, 0) & @CRLF
    $sData &= "--" & $sBoundary & @CRLF
    $sData &= 'Content-Disposition: form-data; name="language"' & @CRLF & @CRLF
    $sData &= "eng" & @CRLF
    $sData &= "--" & $sBoundary & @CRLF
    $sData &= 'Content-Disposition: form-data; name="isOverlayRequired"' & @CRLF & @CRLF
    $sData &= 'true' & @CRLF
    $sData &= "--" & $sBoundary & "--"  & @CRLF

Imagen:

ocr2.png

Error:

Sin_t_tulo.png
 

Share this post


Link to post
Share on other sites

@artepad There is no error in your response. You also could set isOverlayRequired to false to get just the ParsedText without the TextOverlay information.

You can use some Json UDF to parse the response or just use some string manipulation.

Saludos

 

1 person likes this

Share this post


Link to post
Share on other sites

I understand, compile the script with isOverlayRequired false and the string is a little shorter but there is no way I can do that only the number I have to occupy a substring StringMid example to format?

Sin_t_tulo.png

 

I'm sorry, I'm very new to Autoit

Share this post


Link to post
Share on other sites

This returns an array with the numbers inside

$array = StringRegExp($response, '(\d{3}\.\d{3})', 3)

 

1 person likes this

Share this post


Link to post
Share on other sites

I am amazed how easy it was.

Thank you very much! :)

Share this post


Link to post
Share on other sites

thank you too Danyfirex :sweating:

 

Saludos

1 person likes this

Share this post


Link to post
Share on other sites
1 hour ago, Danyfirex said:

thank you too Danyfirex :sweating:

 

Saludos

Jajaja muchas gracias para ti también Danyfirex por ser el primero quien me ayudo :)

 

Saludos!

1 person likes this

Share this post


Link to post
Share on other sites

Hi, it's me again, I have a problem with that line of code only accepts number in "100 thousand" unit but when I have "10 thousand"

the image will have a number from 0 to 100 thousand

Quote
$array = StringRegExp($response, '(\d{3}\.\d{3})', 3)

Example

2.png[/url]

 

I'm trying to understand how StringRegExp works on my part

Share this post


Link to post
Share on other sites

Read the help file.

 

Saludos

1 person likes this

Share this post


Link to post
Share on other sites

Ohhh Danyfirex  :D

Last gift

$array = StringRegExp($response, '(\d+\.\d+)', 3)

But reading the helpfile is not such a bad idea  :)

1 person likes this

Share this post


Link to post
Share on other sites
3 hours ago, mikell said:

Ohhh Danyfirex  :D

Last gift

$array = StringRegExp($response, '(\d+\.\d+)', 3)

But reading the helpfile is not such a bad idea  :)

 

I did not work but thanks for the help and sorry for bothering I will try to learn only with the help  ^_^

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now