0

In CURL, I can use this line

curl --data 'DataToBeSent' https://example.com/resource.cgi

I am struggling to convert such line to be used in VBA and this is my try till now

Sub POST_Method_Example()
Dim myMessage As String

myMessage = "DataToBeSent"

With CreateObject("MSXML2.ServerXMLHTTP")
    .Open "POST", "https://example.com/resource.cgi"

    .setRequestHeader "Content-Type", "multipart/form-data; boundary==------------------------d74496d66958873e"
    .send sData(myMessage)
    Debug.Print .ResponseText
End With
End Sub

But this throws an error at the json response. How can I make this works for VBA?

I can get it work on postman by assigning the url and in Body tab I selected "form-data" and assign KEY: text and VALUE: DataToBeSent This returns a successful response.

YasserKhalil
  • 9,138
  • 7
  • 36
  • 95

1 Answers1

2

See multipart/form-data for when to use.

Sub POST_Method_Example()

    With CreateObject("MSXML2.ServerXMLHTTP")
        .Open "POST", "https://example.com/resource.cgi"
        .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
        .send "text=Data to be sent"
        Debug.Print .ResponseText
    End With

End Sub

CDP1802
  • 13,871
  • 2
  • 7
  • 17
  • Thank you very much for great help. Can you have a look at this link, please? https://stackoverflow.com/questions/60890492/upload-file-to-file-io-using-post-method – YasserKhalil Mar 29 '20 at 16:01