Mac Excel VBA - Problem doing http post via shell

charlieopp

New Member
Joined
May 12, 2015
Messages
2
  1. 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


  2. ' execShell() function courtesy of Robert Knight via StackOverflow
  3. ' osx - VBA Shell function in Office 2011 for Mac - Stack Overflow

  4. Private Declare Function popen Lib "libc.dylib" (ByVal command As String, ByVal mode As String) As Long
  5. Private Declare Function pclose Lib "libc.dylib" (ByVal file As Long) As Long
  6. 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
  7. Private Declare Function feof Lib "libc.dylib" (ByVal file As Long) As Long

  8. Function execShell(command As String, Optional ByRef exitCode As Long) As String
  9. Dim file As Long
  10. Debug.Print "command: " & command
  11. file = popen(command, "r")

  12. If file = 0 Then
  13. Exit Function
  14. End If

  15. While feof(file) = 0
  16. Dim chunk As String
  17. Dim read As Long
  18. chunk = Space(50)
  19. read = fread(chunk, 1, Len(chunk) - 1, file)
  20. If read > 0 Then
  21. chunk = Left$(chunk, read)
  22. execShell = execShell & chunk
  23. End If
  24. Wend

  25. exitCode = pclose(file)
  26. End Function

  27. Sub HTTPpost()

  28. Dim sCmd As String
  29. Dim sResult As String
  30. Dim lExitCode As Long

  31. sCmd = "curl " & _
  32. "-H " & Chr(34) & "content_type:text/plain" & Chr(34) & _
  33. " -d " & Chr(34) & "some data" & Chr(34) & _
  34. " " & "http://httpbin.org/post"

  35. sResult = execShell(sCmd, lExitCode)

  36. ' ToDo check lExitCode
  37. Debug.Print sResult

  38. End Sub



  39. ----------------------
  40. FROM IMMEDIATE WINDOW

  41. call httppost
  42. command: curl -H "content_type:text/plain" -d "some data" http://httpbin.org/post
  43. {
  44. "args": {},
  45. "data": "",
  46. "files": {},
  47. "form": {
  48. "some data": ""
  49. },
  50. "headers": {
  51. "Accept": "*/*",
  52. "Content-Length": "9",
  53. "Content-Type": "application/x-www-form-urlencoded",
  54. "Host": "httpbin.org",
  55. "User-Agent": "curl/7.37.1"
  56. },
  57. "json": null,
  58. "origin": "50.0.134.125",
  59. "url": "http://httpbin.org/post"
  60. }

  61. --------------
  62. FROM SHELL

  63. admins-MacBook-Pro:~ admin$ curl -H "content-type:text/plain" -d "test curl" http://httpbin.org/post
  64. {
  65. "args": {},
  66. "data": "test curl",
  67. "files": {},
  68. "form": {},
  69. "headers": {
  70. "Accept": "*/*",
  71. "Content-Length": "9",
  72. "Content-Type": "text/plain",
  73. "Host": "httpbin.org",
  74. "User-Agent": "curl/7.37.1"
  75. },
  76. "json": null,
  77. "origin": "50.0.134.125",
  78. "url": "http://httpbin.org/post"
  79. }
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Re: Mac Excel VBA - Problem doing http post via shell - SOLVED

SOLVED: Had underscore in header field. Should be content-type:text/plain NOT content_type:text/plain
 
Upvote 0

Forum statistics

Threads
1,214,821
Messages
6,121,759
Members
449,048
Latest member
excelknuckles

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top