weskpga weskpga - 1 year ago 186
HTTP Question

Sending values as parameters in HTTP GET using VBA request

So I am trying to send a basic request to a new API I'm testing out with the following script:

Sub CalcDemo()
TargetURL = "https://my-api-url.com"
Set HTTPReq = CreateObject("WinHttp.WinHttpRequest.5.1")
HTTPReq.Open "GET", TargetURL, False
HTTPReq.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"

postData = "user=myUsername&password=myPassword"
HTTPReq.send (postData)
MsgBox (HTTPReq.responseText)

End Sub

But I'm getting the following error message:
HTTP Status 401 - user and password should be specified as parameters on the request
. I was under the impression that the manner in which
is being passed above meant they are sent as parameters, but I guess I am wrong. How can I send a string of parameters?

m  e m e
Answer Source

As one comment has suggested, you are trying to send a post string usin a GET command. If you replace the folowing line, it should work:

    HTTPReq.Open "GET", TargetURL, False

replaced by:

    HTTPReq.Open "POST", TargetURL, False
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download