0

I have to send a json post via VBA. Here is a sample post:

curl\

-d '{"@SOURCE":"A1","@DESTINATION":"B1","apikey":"SDFLIEJLE....DLKFJSLKDJF"}'\

-H "Content-Type: application/json"\

-X POST https://12345678.net:8443/workflow/0

I know, I have use WinHttpRequest with VBA. But I have no idea how to combine the post in correct way with sub. Here a sample sub for WinHttpRequest:

Sub PostJSON()

  Dim URL As String
  Dim JSONString As String
  Dim objHTTP As New WinHttpRequest
 
URL = "https://api.knack.com/v1/objects/object_1/records"
  objHTTP.Open "POST", URL, False
  objHTTP.SetRequestHeader "X-Knack-Application-Id", "######"
  objHTTP.SetRequestHeader "X-Knack-REST-API-KEY", "######"
  objHTTP.SetRequestHeader "Content-Type", "application/json"

  JSONString = "{""field_1"":""1 Smith St, Smithville""}"
  objHTTP.Send JSONString

End Sub

I don't understand the microsoft docu. https://learn.microsoft.com/en-us/windows/win32/winhttp/winhttprequest

Thank you very much for help.

Best regards

Alexander

AlexTM1
  • 9
  • 3
  • How about just setting `JSONString = ` to the JSON data structure you have in the first part of your post? Is that not working? Your post is unclear for what your actual problem is. – PeterT Jul 07 '20 at 17:41
  • What happens when you run that code? Are you checking the response from the server? – Tim Williams Jul 07 '20 at 17:42
  • @PeterT yes, I think also but I don't know exactly format. Is that the right way? "JSONString = "curl\ -d '{"@SOURCE":"A1","@DESTINATION":"B1","apikey":"SDFLIEJLE....DLKFJSLKDJF"}'\ -H "Content-Type: application/json"\ -X POST https://12345678.net:8443/workflow/0" – AlexTM1 Jul 08 '20 at 05:21
  • @ Tim Williams, if code is correctly, I send a task to application. No response necessary in this case. – AlexTM1 Jul 08 '20 at 05:23

1 Answers1

0
Sub PostJSON()

  Dim URL As String
  Dim JSONString As String
  Dim objHTTP As New WinHttpRequest
 
  URL = "https://12345678.net:8443/workflow/0"
  objHTTP.Open "POST", URL, False
  objHTTP.SetRequestHeader "Content-Type", "application/json"

  JSONString = "{""@SOURCE"":""A1"",""@DESTINATION"":""B1"", ""apikey"":""SDFLIEJLE....DLKFJSLKDJF""}"
  objHTTP.Send JSONString

  'typically here you would check the response to make sure
  '   the post was successfull....
End Sub
Tim Williams
  • 154,628
  • 8
  • 97
  • 125