对于产品抓取项目,我需要在VBA中使用XMLHTTP在GET请求的标头中传递一个值。从先前的XMLHTTP GET请求(getReponseHeader)中提取需要传递的值/令牌,并将其存储在VBA中的变量中。
在PUT请求中使用setRequestHeader传递值时,正确传递了正确的值(之后使用getReponseHeader进行检查时)。但是,当在GET请求中发送值时,它不起作用(提取了不同的令牌)。
通过将正确的商店信息与活动会话连接(通过在标头中发送令牌),可以提取链接到特定商店的产品的特定价格。对于样品产品,我需要的价格为1,87。当没有商店链接到会话时,将提取1.97的价格。这就是为什么我需要在以下GET请求中发送带有正确的x-jumbo-token的标头信息。
这是我需要做的:
这是我当前拥有的代码。
Sub GetJumboPriceXML()
Dim XMLReq As New MSXML2.XMLHTTP60
Dim JumboPrice As Double
Dim SKU_tag As String, SKU_url As String
Dim Jumbo_token As String
Dim Body As String
'sample product (MARS 225gr) --> price 1,97 (when no store is selected), 1,87 when correct store is linked (store id 3065, complexNumber 33164)
SKU_tag = "153501PAK"
SKU_url = "http://mobileapi.jumbo.com/v2/products/" & SKU_tag
'get the x-jumbo-token value from the configuration page
XMLReq.Open "GET", "http://mobileapi.jumbo.com/v2/configuration", False
XMLReq.send
'store in variable
Jumbo_token = XMLReq.getResponseHeader("x-jumbo-token")
Debug.Print Jumbo_token
send JSON body with store info together with x-jumbo-token in header to select correct store
Body = "{""id"":""3065"", ""complexNumber"":""33164""}"
XMLReq.Open "PUT", "http://mobileapi.jumbo.com/v2/users/me/homestore", False
XMLReq.setRequestHeader "x-jumbo-token", Jumbo_token
XMLReq.send (Body)
'check on response value of x-jumbo-token (is equal to the token I sent in header), all seems fine until this point
Debug.Print XMLReq.getResponseHeader("x-jumbo-token")
'send final request to product page and extract the price info
XMLReq.Open "GET", SKU_url, False
XMLReq.setRequestHeader "x-jumbo-token", Jumbo_token
XMLReq.send
JumboPrice = Split(Split(XMLReq.responseText, "prices"":{""price"":{""currency"":""EUR"",""amount"":")(1), "}")(0) / 100
'debug print product price > 1,97 is displayed instead of 1,87 (when the correct store is connected to the session).
Debug.Print JumboPrice
'x-jumbo-token from RepsonseHeader is different vs. the one I sent as header with the request.
Debug.Print XMLReq.getResponseHeader("x-jumbo-token")
End Sub
有人建议我如何在后续的GET请求中传递正确的令牌,并在最终请求的getResponseHeader中提取相同的x-jumbo-token,并提取正确的价格1,87?