MSXML2.XMLHTTP not working in company network - excel

I've built a macro for a client that POSTs some data to an external URL which requires basic authentication (get/save data in cloud database via api). When I'm running the macro from home (or basically anywhere) it works perfectly. However, when my client runs it (while in his company network), he gets a timeout error message after a few seconds. Any suggestions on what the cause could be?
Here is my code:
Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
With objHTTP
.Open "POST", url, False
.SetRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
.SetRequestHeader "Content-Type", "application/json"
.SetRequestHeader "Accept", "application/json"
.SetRequestHeader "Authorization", "Basic " + Base64EncodeString(apiKey + ":" + apiPw)
.Send (data)
End With

Related

Error throwing when connecting api with client I'd and key

I'm trying to access data through an API that takes in a X-client-id and x-api-key. I've tried the following so far but it's not working. Can anyone help?
Dim request as new winhttprequest
Request.open "Get", url, False
Request.setrequestheader "Accept", "application/json"
Request.setrequestheader "X-client-id", "[Id]"
Request.setrequestheader "x-api-key", "[key]"

XMLHttpRequest from VBA returns error 406 "Not Accepted"

I'm trying to do some scraping using Excel VBA, specifically, to get a JSON from a server.
I'm using exactly the same request headers that show on the browsers dev tools, but when I do this request from Excel I always get a 406 ("Not accepted") answer from the server.
What am I missing?
Sub getJSON()
Dim XHR As New MSXML2.ServerXMLHTTP60
XHR.Open "GET", "https://www.magazineluiza.com.br/produto/calculo-frete/08226021/615254900/frigelar.json", False
XHR.setRequestHeader "Host", "www.magazineluiza.com.br"
XHR.setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0"
XHR.setRequestHeader "Accept", "application/json, text/javascript, */*; q=0.01"
XHR.setRequestHeader "Accept-Language", "pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3"
XHR.setRequestHeader "Accept-Encoding", "gzip, deflate, br"
XHR.setRequestHeader "X-Requested-With", "XMLHttpRequest"
XHR.setRequestHeader "Connection", "keep-alive"
XHR.setRequestHeader "Referer", "https://www.magazineluiza.com.br/refrigerador-expositor-para-bebidas-metalfrio-com-controlador-eletronico-497-litros-vb52r-220v/p/6152549/pi/expb/"
XHR.setRequestHeader "TE", "Trailers"
XHR.send
Debug.Print XHR.responseText
End Sub
This is the answer I get on the browser:
{"address":{"address":"18 DE ABRIL","area":"CIDADE ANTONIO ESTEVAO DE CARVALHO","city":"SAO PAULO","state":"SP","zip_code":"08226021"},"delivery":[{"description":"Entrega padrĂ£o","distribution_center":0,"is_deadline":true,"price":"263.91","time":13,"zip_code_restriction":false}],"is_delivery_unavailable":false,"zip_code":"08226021"}
Edit: This JSON is retrieved by the browser when I add the zip code "08226021" on the right side of this page and click "Ok".
Try this to get the required response:
Sub FetchJsonResponse()
Const Url$ = "https://www.magazineluiza.com.br/produto/calculo-frete/08226021/615254900/frigelar.json"
With CreateObject("WinHttp.WinHttpRequest.5.1")
.Open "GET", Url, False
.setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.1; ) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36"
.setRequestHeader "Referer", "https://www.magazineluiza.com.br/refrigerador-expositor-para-bebidas-metalfrio-com-controlador-eletronico-497-litros-vb52r-220v/p/6152549/pi/expb/"
.setRequestHeader "x-requested-with", "XMLHttpRequest"
.send
Debug.Print .responseText
End With
End Sub
This is the output it produces:
{"address": {"address": "18 DE ABRIL", "area": "CIDADE ANTONIO ESTEVAO DE CARVALHO", "city": "SAO PAULO", "state": "SP", "zip_code": "08226021"}, "delivery": [{"description": "Entrega padr\u00e3o", "distribution_center": 0, "is_deadline": true, "price": "263.91", "time": 13, "zip_code_restriction": false}], "is_delivery_unavailable": false, "zip_code": "08226021"}

VBA HTTP Request showing runtime error in different system

I am facing error for my VBA(HTTP Request) code. while in my another system it is working fine.
system(windows-8 /office-365): Problem (runtime error:'-2147024809 (80070057)': The Parameter is incorrect.)
system(windows-10 /office-16): No Problem
References are same in both systems.
Please help me..!!
enter image description here
Here is the Code:
Dim cook As String
Dim body1 As String
Dim xmlhttp As Object
Set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP.6.0")
cook = Worksheets("Action").Range("I2").Value
body1 = Worksheets("Action").Range("I3").Value
URL = "https://trade.xxxxxxx.in/ITSStreamer/streamer/UserManagementServlet"
xmlhttp.Open "POST", URL, False
xmlhttp.setRequestHeader "Host", "trade.xxxxxxx.in"
xmlhttp.setRequestHeader "Connection", "keep-alive"
xmlhttp.setRequestHeader "Content-Length", "700"
xmlhttp.setRequestHeader "Accept", "application/json, text/javascript, */*; q=0.01"
xmlhttp.setRequestHeader "Origin", "https://trade.xxxxxxx.in"
xmlhttp.setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"
xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"
xmlhttp.setRequestHeader "Referer", "https://trade.xxxxxxx.in/NEWITS/InterfaceToValidateToken.do?"
xmlhttp.setRequestHeader "Accept-Encoding", "gzip, deflate, br"
xmlhttp.setRequestHeader "Accept-Language", "en-US,en;q=0.9"
xmlhttp.setRequestHeader "Cookie", cook
xmlhttp.send body1
Debug.Print xmlhttp.ResponseText

How to scrape the HTTP header? (Redirect URL?)

I've installed an add-on in my browser which let me see all the HTTP headers that go back and forth from my Firefox and a web server.
This is a simple code I've used:
Set WinHttpReq = CreateObject("MSXML2.ServerXMLHTTP")
WinHttpReq.Open "GET", "Initial_URL.com", False
WinHttpReq.Send
Below is the http header obtained:
https://Initial_URL.com
Host: Initial_URL.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
GET: HTTP/1.1 302 Moved Temporarily
content-length: 2361
content-type: text/html
date: Wed, 09 Jan 2019 19:46:06 GMT
location: Redirected_to.com
Set-Cookie: PD-S-SESSION-ID=1_2_0_ud2NmPnh++62VQCPVkwxb8xp0wuMBDhqmfZiqltbPrgksUAf;
First part of the header is what the HTTP header has sent to the server, and then the answer from the server.
I need to get some of these fields (e.g. location, Set-Cookie).
I've parsed the WinHttpReq.ResponseText, but it is pure HTTP code, no headers at all.
Any guidance?
If you want the header information, you can get that from the getAllResponseHeaders method.
Here is a small demo:
Sub GetHeaders()
Dim headers As String
With CreateObject("MSXML2.ServerXMLHTTP")
.Open "GET", "https://stackoverflow.com/questions/54118535/excel-vba-how-to-scrape-the-http-header-webserver-answer-redirect-url"
.send
headers = .getAllResponseHeaders
Debug.Print headers
End With
End Sub

Is it possible to convert a cURL request to a VBA winHttpRequest?

I have the request below, trying to get a paypal auth token to retrieve a transaction list from my account.
curl -v https://api.sandbox.paypal.com/v1/oauth2/token \
-H "Accept: application/json" \
-H "Accept-Language: en_US" \
-u "client_id:secret" \
-d "grant_type=client_credentials"
And I have gone this far:
Set objHTTP = New WinHttp.WinHttpRequest
sURL = "https://api.sandbox.paypal.com/v1/oauth2/token"
objHTTP.Open "POST", sURL, False
'what should go here? is the POST request correct? Are -H in cURL for headers?
objHTTP.send (sBody)
Can you help me get this working? Is there maybe an easier way to have the cURL requests "converted"?
EDIT:
sURL = "https://api.sandbox.paypal.com/v1/oauth2/token"
objHTTP.Open "POST", sURL, False
objHTTP.SetRequestHeader "Content-Type", "application/json"
objHTTP.SetRequestHeader "Accept", "application/json"
User = "user"
Password = "password"
objHTTP.SetRequestHeader "Authorization", "Basic " & Base64Encode(User + ":" + Password)
Runtime Error 5
Invalid procedure call or arguement. Any ideas?
Here's a working example. I don't run Excel anymore so I can't get at the code but it's in there, just open the macro editor. This uses the MSXML library to do an XHR fetch from a REST API.
https://rack.pub/cdn/miles.xlam

Resources