GET request data doesn't update

后端 未结 2 1678
故里飘歌
故里飘歌 2021-01-15 02:35

I\'m trying to retrieve data from an API but my variable doesn\'t update even if I set it as nothing before the GET request.

The data of the variable update only if

2条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-15 03:35

    You can add an instruction to avoid being served cached results (server can ignore this but I have had good success with this in the past). Make sure your async argument is always False and leave more time between tests. I notice that sometimes the prices are slow to change so you may miss the change due to too small an interval/not enough attempts. You will notice the size change though. You should add a max timeout to the loop in the bottom script.

    Also removed the hungarian notation.

    Option Explicit
    
    Public Sub getJsonResult()
        Dim http As Object
        Dim urlXBTUSD As String
        Dim response As String
        Dim j As Long
        Const ASYNC_ARG As Boolean = False
    
        Set http  = CreateObject("MSXML2.XMLHTTP")
        For j = 1 To 10
            response = vbNullString
            urlXBTUSD = "https://www.bitmex.com/api/v1/orderBook/L2?symbol=XBTUSD&depth=3"
    
            With http 
                .Open "GET", urlXBTUSD,  ASYNC_ARG
                .setRequestHeader "Content-Type", "application/json"
                .setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
                .send
                response = .responseText
                Debug.Print response
            End With
            Application.Wait Now + TimeSerial(0, 0, 15)
        Next
    End Sub
    

    Here is a long and tedious way of proving it by looping until price of first item in return collection changes. I use jsonconverter.bas added to project and VBE > Tools > References > Microsoft Scripting Runtime reference.

    Option Explicit
    
    Public Sub getJsonResult()
        Dim http  As Object
        Dim urlXBTUSD As String
        Dim response As String
        Dim j As Long
        Const ASYNC_ARG As Boolean = False
        Dim price As String, firstValue As String
    
        Set http  = CreateObject("MSXML2.XMLHTTP")
        urlXBTUSD = "https://www.bitmex.com/api/v1/orderBook/L2?symbol=XBTUSD&depth=3"
    
        With http 
            .Open "GET", urlXBTUSD,  ASYNC_ARG
            .setRequestHeader "Content-Type", "application/json"
            .setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
            .send
             firstValue = JsonConverter.ParseJson(.responseText)(1)("price")
            Debug.Print  firstValue
            Do
                .Open "GET", urlXBTUSD, blnAsync
                .setRequestHeader "Content-Type", "application/json"
                .setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
                .send
                price = JsonConverter.ParseJson(.responseText)(1)("price")
                Application.Wait Now + TimeSerial(0, 0, 5)
            Loop While price = firstValue
            Debug.Print price
        End With
    End Sub
    

提交回复
热议问题