vbascripting.dictionary

VBA webResponse Response.data getting error when changing type from "Dictionary" to "collection"


Here is the problem : I have the following code

Dim Client As New WebClient
Dim Request As New WebRequest
Dim Response As WebResponse
    
Dim responseData As Scripting.Dictionary
    
Request.UserAgent = VBA.Environ("USERNAME")
Request.AddHeader "AuthToken", m_token
Request.AddHeader "APIKey", m_key
Request.Method = WebMethod.HttpPost
    
Dim Auth As New HttpBasicAuthenticator
Auth.Setup "username", "Password"
Set Client.Authenticator = Auth
    
Dim Body As New Dictionary
If condition1 Then
        Body.Add "status", "open"
Else
        Body.Add "status", "closed"
End If
    
Client.BaseUrl = server_api & "Tasks/" & m_id
    
Set Request.Body = Body
Set Response = Client.Execute(Request)
    
If (Response.StatusCode = Ok) Then
        Debug.Print TypeName(Response.Data) 'returns Dictionary
        Set responseData = Response.Data '.Item(1)
        Debug.Print "ID: " & responseData("id")
        Debug.Print "message: " & responseData("message")
End If

The request is executed with success, the program has no errors, and the Response.data has the typename Dictionary, but message is :

'statut' doesn't exist in table

So I tried to fix this by changing the "status" name to the required "tableColunm" which have int32 as type in the database.

The changes applied are in the following code with results on comments:

    If condition1 Then
        Body.Add "tableColunm", 1 'I tried also with "1"
    Else
        Body.Add "tableColunm", 2 ' I tried also with "2"
    End If        Client.BaseUrl = server_api & "Tasks/" & m_id
    
    Set Request.Body = Body
    Set Response = Client.Execute(Request)
    
    If (Response.StatusCode = Ok) Then
        Debug.Print TypeName(Response.Data) 'returns Collection
        Set responseData = Response.Data '.Item(1) ' error here, and the program crashed
        Debug.Print "ID: " & responseData("id")
        Debug.Print "message: " & responseData("message")
    End If

Now the typename of Response.data is collection, and I think this is the reason why the program crashes

Any help ? Thanks


Solution

  • Finally, after adding this

    For Each elt In Response.Data
        Debug.Print TypeName(elt)
    Next
    

    The loop is executed only one time, outputting Dictionary. That's mean that in the second case, the dictionary is encapsulated on a collection

    So I worked with elt and this solves my problem