charlieopp
New Member
- Joined
- May 12, 2015
- Messages
- 2
- Hi all,
I'm trying to execute an HTTP post on Mac Excel via VBA. I found some code on SO which mostly works but the treatment of the keyword parameters does not work. See code below. See test execution of code further below where I'm posting to an echo site; debug.print shows the command properly constructed. And the same command from the command shell works properly at bottom.
Any ideas?
Thanks,
Charlie
Option Explicit
-
- ' execShell() function courtesy of Robert Knight via StackOverflow
- ' osx - VBA Shell function in Office 2011 for Mac - Stack Overflow
-
- Private Declare Function popen Lib "libc.dylib" (ByVal command As String, ByVal mode As String) As Long
- Private Declare Function pclose Lib "libc.dylib" (ByVal file As Long) As Long
- Private Declare Function fread Lib "libc.dylib" (ByVal outStr As String, ByVal size As Long, ByVal items As Long, ByVal stream As Long) As Long
- Private Declare Function feof Lib "libc.dylib" (ByVal file As Long) As Long
-
- Function execShell(command As String, Optional ByRef exitCode As Long) As String
- Dim file As Long
- Debug.Print "command: " & command
- 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
-
- Sub HTTPpost()
-
- Dim sCmd As String
- Dim sResult As String
- Dim lExitCode As Long
-
- sCmd = "curl " & _
- "-H " & Chr(34) & "content_type:text/plain" & Chr(34) & _
- " -d " & Chr(34) & "some data" & Chr(34) & _
- " " & "http://httpbin.org/post"
-
- sResult = execShell(sCmd, lExitCode)
-
- ' ToDo check lExitCode
- Debug.Print sResult
-
- End Sub
-
-
-
- ----------------------
- FROM IMMEDIATE WINDOW
-
- call httppost
- command: curl -H "content_type:text/plain" -d "some data" http://httpbin.org/post
- {
- "args": {},
- "data": "",
- "files": {},
- "form": {
- "some data": ""
- },
- "headers": {
- "Accept": "*/*",
- "Content-Length": "9",
- "Content-Type": "application/x-www-form-urlencoded",
- "Host": "httpbin.org",
- "User-Agent": "curl/7.37.1"
- },
- "json": null,
- "origin": "50.0.134.125",
- "url": "http://httpbin.org/post"
- }
-
- --------------
- FROM SHELL
-
- admins-MacBook-Pro:~ admin$ curl -H "content-type:text/plain" -d "test curl" http://httpbin.org/post
- {
- "args": {},
- "data": "test curl",
- "files": {},
- "form": {},
- "headers": {
- "Accept": "*/*",
- "Content-Length": "9",
- "Content-Type": "text/plain",
- "Host": "httpbin.org",
- "User-Agent": "curl/7.37.1"
- },
- "json": null,
- "origin": "50.0.134.125",
- "url": "http://httpbin.org/post"
- }