How can I send an HTTP POST request to a server from Excel using VBA?

前端 未结 6 1923
执笔经年
执笔经年 2020-11-22 08:01

What VBA code is required to perform an HTTP POST from an Excel spreadsheet?

6条回答
  •  伪装坚强ぢ
    2020-11-22 08:43

    To complete the response of the other users:

    For this I have created an "WinHttp.WinHttpRequest.5.1" object.

    Send a post request with some data from Excel using VBA:

    Dim LoginRequest As Object
    Set LoginRequest = CreateObject("WinHttp.WinHttpRequest.5.1")
    LoginRequest.Open "POST", "http://...", False
    LoginRequest.setRequestHeader "Content-type", "application/x-www-form-urlencoded"
    LoginRequest.send ("key1=value1&key2=value2")
    

    Send a get request with token authentication from Excel using VBA:

    Dim TCRequestItem As Object
    Set TCRequestItem = CreateObject("WinHttp.WinHttpRequest.5.1")
    TCRequestItem.Open "GET", "http://...", False
    TCRequestItem.setRequestHeader "Content-Type", "application/xml"
    TCRequestItem.setRequestHeader "Accept", "application/xml"
    TCRequestItem.setRequestHeader "Authorization", "Bearer " & token
    TCRequestItem.send
    

提交回复
热议问题