0

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 postData is being passed above meant they are sent as parameters, but I guess I am wrong. How can I send a string of parameters?

2
  • 1
    That is not a GET but a POST, so HTTPReq.Open "POST", TargetURL, False Commented Sep 9, 2016 at 0:36
  • 1
    To add - if it were a GET you'd be doing something like: TargetURL = "https://my-api-url.com?user=myUsername&password=myPassword" (ie. parameters are passed in the querystring portion of the URL). In a POST, the parameters are passed in the request body. Commented Sep 9, 2016 at 5:59

2 Answers 2

2

It seems to me that the value of postData is not collecting the values of the variables myUsername or myPassword. you have to create the concatenated data like so:

postData = "user=" & myUsername & "&password=" & myPassword

I suppose that myUsername and myPassword are global variables. Otherwise you also need to pass them as arguments to your function.

Hope this helps!

Sign up to request clarification or add additional context in comments.

2 Comments

So I tried postData = "user=" & "myUsername" & _ "&password=" & "myPassword" & _ "&param1=" & "param1val" & _ "&param2=" & "param2val" & _ .... But am still getting the same error. Just by a cursory check, the params look to be formatted properly.
And yeah, I just changed the username and password to placeholders :)
1

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

1 Comment

Looks like this is the right answer. Interesting because I did the same thing as a GET request in python and it worked, but the POST request gave me this error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.