Convert CURL command line to VBA

2020-05-08 07:14发布

问题:

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.

回答1:

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