0

I have the following curl code which I am trying to convert to VBA for excel

   Endpoint : http://api.datadoctorit.com/email-validate
   Request Type : POST
   curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Content-Length: ' . strlen( $data ),
            'X-Auth-Token: XXXXXXX'
        ) );

Here is the code that I came up with:

Dim strURL As String
Dim hReq As WinHttpRequest: Set hReq = CreateObject("WinHttp.WinHttpRequest.5.1")
Dim strResp As String
Dim Token As String
Token = mytoken
strURL = "http://api.datadoctorit.com/email-validate"
hReq.Open "POST", strURL, False
hReq.SetRequestHeader "Content-Type", "application/json"
hReq.Send "X-Auth-Token" & ":" & token
strResp = hReq.ResponseText
MsgBox strResp

I keep getting this error:

" {""error"":""You need to sign in or sign up before continuing.""}"

Dave Branson
  • 5
  • 1
  • 4
  • 1
    `X-Auth-Token` is a header so should probably look like `hReq.SetRequestHeader "X-Auth-Token", token` followed by a `hReq.Send` – ccpizza May 27 '17 at 16:04
  • Tried the following variation with the same result: hReq.SetRequestHeader "X-Auth-Token" , token – Dave Branson May 27 '17 at 20:49
  • Have a look at https://stackoverflow.com/questions/158633/how-can-i-send-an-http-post-request-to-a-server-from-excel-using-vba – ccpizza May 27 '17 at 22:35

1 Answers1

1

Adding authentication can be done with your Base 64 authorization (generated from the username and password of your basic authorization API call)

Just add the following:

hReq.setRequestHeader "Authorization", "Basic " & authCode

eg.

hReq.SetRequestHeader "Content-Type", "application/json"
hReq.setRequestHeader "Authorization", "Basic cHBzX2NyZWdJIYXAhjhjdjsfhjdshakjfdhakfhjk5c0AjMTZAY3JlZ"

(a real Auth code is about 50 characters long)