Yerry Balenzuela
Yerry Balenzuela

Reputation: 43

how do i use this curl in excel vba to authenticate and receive session key

this curl is used to recieve session key from api

    curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{
  "UserId": "ID",
  "Password": "PW"
}' 'https://website.com/api/v1/Login'

i need to have the session key generated in excel to use with other codes i wrote. this is what i have so far.

    Sub GenAPISKey()

  Dim webServiceURL As String
  Dim actionType As String
  Dim targetWord As String
  Dim actionType2 As String
  Dim targetWord2 As String
  Dim UserID As String
  Dim Password As String

  webServiceURL = "https://website.com/api/v1/Login"
  actionType = "Accept"
  targetWord = "application/json"
  actionType2 = "Content-Type"
  targetWord2 = "application/json"
  UserID = "ID"
  Password = "PW"


  With CreateObject("Microsoft.XMLHTTP")
    .Open "POST", webServiceURL, False
    .SetRequestHeader actionType, targetWord
    .SetRequestHeader actionType2, targetWord2
    .SetCredentials "UserID", "Password", 0

    .Send
    If .Status = 200 Then
      Debug.Print .responseText

    Else
      MsgBox .Status & ": " & .StatusText
    End If
  End With

End Sub

Any ideas on what i can try?the Curl works perfect its the vba version of it i can't get to work with the api.

Upvotes: 0

Views: 2063

Answers (1)

ArcherBird
ArcherBird

Reputation: 2134

It appears that .SetCredentials isn't a member of the XMLHTTP object you are using. I have two suggestions.

1) use a different object like the WinHttpRequest object:

With CreateObject("WinHttp.WinHttpRequest.5.1")
    .Open "POST", webServiceURL, False
    .SetRequestHeader actionType, targetWord
    .SetRequestHeader actionType2, targetWord2
    .SetCredentials "UserID", "Password", 0
    .Send
    If .Status = 200 Then
      Debug.Print .responseText

    Else
      MsgBox .Status & ": " & .StatusText
    End If
  End With

2) If you must use the XMLHTTP object, try setting the credentials in the .Open method call and get rid of the .SetCredentials line.

With CreateObject("Microsoft.XMLHTTP")
    .Open "POST", webServiceURL, False, "UserID", "Password"
    .SetRequestHeader actionType, targetWord
    .SetRequestHeader actionType2, targetWord2

    .Send
    If .Status = 200 Then
      Debug.Print .responseText

    Else
      MsgBox .Status & ": " & .StatusText
    End If
  End With

Edit:

Seams like option one is the way to go for you. Now, I believe you just need to send through the body of JSON that your API wants. In fact, you probably don't even need to set credentials if this is how the API is working. Looking at your -curl command, you should try....:

With CreateObject("WinHttp.WinHttpRequest.5.1")
    .Open "POST", webServiceURL, False
    .SetRequestHeader actionType, targetWord
    .SetRequestHeader actionType2, targetWord2

    .Send "{""UserId"": ""ID"", ""Password"": ""PW""}"
    If .Status = 200 Then
      Debug.Print .responseText

    Else
      MsgBox .Status & ": " & .StatusText
    End If
  End With

Upvotes: 1

Related Questions