aa2zz6

HTTPS Post and Request

5 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

 

venezuela.png Would you like to say thank to Danyfirex using a warmy way?  offer me a cup of coffee... coffee.png

autoit_scripter_blue_userbar.png

       AutoIt...

 

 

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

 

venezuela.png Would you like to say thank to Danyfirex using a warmy way?  offer me a cup of coffee... coffee.png

autoit_scripter_blue_userbar.png

       AutoIt...

 

 

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

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