Jump to content

HTTPS Post and Request


Recommended Posts

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
Link to comment
Share on other sites

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

Link to comment
Share on other sites

@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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

  • 1 month later...

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
 

Link to comment
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

 

Link to comment
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

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...