VBA-tools / VBA-Web

VBA-Web: Connect VBA, Excel, Access, and Office for Windows and Mac to web services and the web
http://vba-tools.github.io/VBA-Web/
MIT License
2.01k stars 494 forks source link

PrepareCurlRequest failed #394

Closed Amertz08 closed 5 years ago

Amertz08 commented 5 years ago

Getting this error on an API request.

ERROR - WebClient.Execute: -2147210493 (11011 / 80042b03), An error occurred during execute
-2147210491 (80042b05): Method 'PrepareCurlRequest' of object 'WebClient' failed

This is the code being invoked generalized

Private Const APIKey = "apikey"
Sub TEST()
    Dim Client As New WebClient
    Client.BaseUrl = "https://some-api.com"
    Dim Request As New WebRequest
    Request.Resource = "files"
    Request.Method = WebMethod.HttpPost
    Request.ResponseFormat = WebFormat.JSON

    Dim Headers As New Collection
    Headers.Add "email_address"

    Dim File As New Dictionary
    File.Add "name", RandomString(5) & ".csv"
    File.Add "headers", Headers
    File.Add "num_rows", 2

    Dim Body As New Dictionary
    Body.Add "api_key", APIKey
    Body.Add "file", File

    Set Request.Body = Body
    Dim Response As WebResponse
    Set Response = Client.Execute(Request)
End Sub

I've went thought the code flow using Debug.Print to determine on what calls it is breaking. It appears to be emanating from here. The final failure point appears to be here where it sets Key to "User-Agent".

For the life of me I can't tell why this would be causing any issues but it is. The JSON struct should look as so

{
    "api_key": "XXX",
    "file": {
        "name": "file.csv",
        "headers": ["email_address"],
        "num_rows": 2,
    }
}

Max OSX: 10.14.3 Excel: 16.23 VBA-Web: 4.1.6

Amertz08 commented 5 years ago

Currently I believe this issue is caused by that array on "headers". Is this the VBA provided the correct way to structure the request?

zgrose commented 5 years ago

Not running on a Mac (at the moment), but you can help Tim out if you had the actual error message and tried to debug the root cause (object is Nothing, value is null, etc, etc). I'd imagine if you could illustrate the cause of the error message, you could illuminate the solution to the problem.

Amertz08 commented 5 years ago

That is the only error message I've seen. PrepareCurlRequest fails here and then I traced via Debug.Print to where the underlying failure is to it setting the user-agent key.

Amertz08 commented 5 years ago

I did attempt to just convert the request body to JSON (via ConvertToJson) prior to assigning it to the body and that also failed. Gave me a "Object does not support this property or method" and then showed me this line.

Amertz08 commented 5 years ago

Yeah so finally figured it out for the most part. I'm not sure why but assigning a value to a dictionary via Dic("A") = "val" does not work in WebHelpers.bas

Dictionary 1.4.1

I was able to get past this by using .Add "key", "value" syntax.

Amertz08 commented 5 years ago

I was able to verify that this is also an issue in Windows in Office 365. The following lines throw Object does not support this property or method.

Public Function CreateKeyValue(Key As String, Value As Variant) As Dictionary
    Dim web_KeyValue As New Dictionary

    web_KeyValue("Key") = Key ' error here
    web_KeyValue("Value") = Value
    Set CreateKeyValue = web_KeyValue
End Function
Amertz08 commented 5 years ago

I realized I had not imported my .cls files correctly. Purged my project and imported them again. This resolved it.