I'm trying to send an image file via curl via Mac Excel VBA. I've gotten great help from How do I issue an HTTP GET from Excel VBA for Mac and I have most of it working. I can send data via post and get server responses.
However, the code fails when I try to upload a file.
Here is an example of a curl string which executes perfectly from the Terminal app:
curl -H "apikey:helloworld" --form "file=@/Various/ImageTest/Puzzle2.smaller.jpg" https://api.ocr.space/Parse/Image
Note the pathname has to start with an "@" and the top level is the root directory. Also the image file is an image on my Mac, but if you try to test the above just point to any image file on yours.
The server responds to the above.
However, when I take the same code and I put it into VBA it fails. I am succeeding in everything other than loading a file. But when I try to upload a file I get a code 6656 returned and nothing else. I don't believe the 6656 is coming from the server, I think it's coming right from my Mac.
Here is an example
Sub RunTest2()
Dim result As String
Dim exitCode As Long
Dim Stringtest As String
Dim StringToSend As String
StringToSend = "curl -H 'apikey:helloworld' --form 'file=@/Various/ImageTest/Puzzle2.smaller.jpg' https://api.ocr.space/Parse/Image"
result = execShell(StringToSend, exitCode)
Debug.Print "Result: " & result
Debug.Print "Exit Code: " & Str(exitCode)
End Sub
The execShell routine above is the same one from How do I issue an HTTP GET from Excel VBA for Mac. Here is that code:
Option Explicit
' execShell() function courtesy of Robert Knight via StackOverflow
' http://stackoverflow.com/questions/6136798/vba-shell-function-in-office-2011-for-mac
Private Declare PtrSafe Function popen Lib "libc.dylib" (ByVal command As String, ByVal mode As String) As LongPtr
Private Declare PtrSafe Function pclose Lib "libc.dylib" (ByVal file As LongPtr) As Long
Private Declare PtrSafe Function fread Lib "libc.dylib" (ByVal outStr As String, ByVal size As LongPtr, ByVal items As LongPtr, ByVal stream As LongPtr) As Long
Private Declare PtrSafe Function feof Lib "libc.dylib" (ByVal file As LongPtr) As LongPtr
Function execShell(command As String, Optional ByRef exitCode As Long) As String
Dim file As LongPtr
file = popen(command, "r")
If file = 0 Then
Exit Function
End If
While feof(file) = 0
Dim chunk As String
Dim read As Long
chunk = Space(50)
read = fread(chunk, 1, Len(chunk) - 1, file)
If read > 0 Then
chunk = Left$(chunk, read)
execShell = execShell & chunk
End If
Wend
exitCode = pclose(file)
End Function
Function HTTPGet(sUrl As String, sQuery As String) As String
Dim sCmd As String
Dim sResult As String
Dim lExitCode As Long
sCmd = "curl --get -d """ & sQuery & """" & " " & sUrl
sResult = execShell(sCmd, lExitCode)
' ToDo check lExitCode
HTTPGet = sResult
End Function
Thinking that maybe the problem was with string format, I've tried double-quotes, single quotes, and ascii chr(34), but nothing changed. Ii also tried the ascii for the @ sign. If I remove the @ then I do get a response from the server but the response is that the "file" parameter is wrong. Somehow the file is not being loaded correctly, even though the same code works from Terminal. I am thinking that it could be a permissions issue. Any help is appreciated.