将 CURL 命令行转换为 VBA [英] Convert CURL command line to VBA

查看:31
本文介绍了将 CURL 命令行转换为 VBA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 CURL 中,我可以使用这一行

In CURL, I can use this line

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

我正在努力将这样的行转换为在 VBA 中使用,这是我迄今为止的尝试

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

但这会在 json 响应中引发错误.我怎样才能使它适用于 VBA?

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

我可以通过分配 url 并在 Body 选项卡中选择form-data"并分配 KEY: text 和 VALUE: DataToBeSent 来让它在邮递员上工作这将返回一个成功的响应.

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.

推荐答案

参见 multipart/form-data 何时使用.

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

这篇关于将 CURL 命令行转换为 VBA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆