VBScript unable to write http.ResponseBody to ADODB Stream - http

I am sending an HTTP POST to an API with a json request to download a zip file. The response contains the zip file in the responseBody. I have verified this sequence in Postman with the responsebody containing the zip file.
I am attempting to save the zip file from the http response body to a local file doing the following in VBScript:
Sub Download_Study_Bundle
URL = myURL
Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
objHTTP.Open "POST", URL, False
objHTTP.setRequestHeader "User-Agent", "Mozilla/4.0"
objHTTP.setRequestHeader "Authorization", "Basic base64encodeduserandpassword"
objHTTP.setRequestHeader "Content-Type", "application/json; charset=UTF-8"
objHTTP.setRequestHeader "CharSet", "charset=UTF-8"
objHTTP.setRequestHeader "Accept", "application/json"
json = myjson
objHTTP.send (json)
dim bStrm: Set bStrm = createobject("Adodb.Stream")
with bStrm
.type = 1 '//binary
.open
.write objHTTP.responseBody
.savetofile myFile, 2 '//overwrite
end with
end sub
I get an error on line : ".write objHTTP.responseBody"
that states:
"Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another."
I can see in my logs that I get a proper 200 response with something in ResponseBody. I am unable to figure out why the responseBody cannot be written as a binary stream to the ADODB object. Any help is appreciated.
EDIT: a screenshot of the same json sent in postman produces the raw zip file in the response body:
http response

Related

WinHttp.WinHttpRequest: The server returned an invalid or unrecognized response

In Lotus a lotusscript agent is using this piece of code from a ScriptLibrary:
Set objDom = CreateObject("MSXML2.DOMDocument")
Set objXmlHttp = CreateObject("WinHttp.WinHttpRequest.5.1")
Call objXmlHttp.SetTimeouts("240000", "240000", "240000", "240000")
objXmlHttp.Option(9) = 2048
objXmlHttp.Option(6) = True
objDom.async = False
objDom.LoadXml XmlBody
objXmlHttp.open "POST", Url, False
objXmlHttp.setRequestHeader "Content-Type", "text/xml; charset=utf-8"
objXmlHttp.setRequestHeader "Content-Length", Len(XmlBody)
Status = objXmlHttp.status
responseText = objXmlHttp.responseText
The code is sending a POST. POST is from HTTPS to HTTPS URL.
When executing this post in the beginning I sometimes received a timeout, so in turn I upped the Timeouts until I am not receiving any timeouts anymore. But now I am sometimes receiving this error:
WinHttp.WinHttpRequest: The server returned an invalid or unrecognized response
When I resend the POST request then it works again. Is there any way to stop this errors?
Before the winHTTP type I always used the MSXML2.XMLHTTP object, but because TLS <1.2 will be depreceted for us, I switched to the winHTTP but this sometimes gives problems.

How do I Access Response Header in Http Request

I'm making a call to an external API. The data it returns, annoyingly, is in the header (the text response is empty).
How do I access the header of the response?
This is what I'm trying:
Dim httpRequest, postResponse
Set httpRequest = Server.CreateObject("MSXML2.ServerXMLHTTP")
httpRequest.Open "POST", "http://www.api.com", False, "un", "pw"
httpRequest.SetRequestHeader "Content-Type", "application/json"
httpRequest.setRequestHeader "Content-Length", len(jsondata)
httpRequest.Send data
if httpRequest.status = 200 then
response.write httpRequest.getResponseHeader
response.write httpRequest.ResponseText
end if
Set httpRequest = nothing
But it gives me:
msxml3.dll error '80072f76'
The requested header was not found
And a bonus question: I just noticed the "XML" part of "MSXML2.ServerXMLHTTP" - am I using the right protocol? It's always worked for straight posts and gets until now.
You need to specify the name of the response header you want to retrieve:
response.write httpRequest.getResponseHeader("SomeHeaderName")
There's no just one response header. There could be many. You have the standard response headers such as Content-Type and you could also have custom headers.
And a bonus question: I just noticed the "XML" part of
"MSXML2.ServerXMLHTTP" - am I using the right protocol?
Yes, absolutely, that's the correct COM object to be used from a classic ASP application to send HTTP requests.

Pingdom REST API from Classic ASP

I would like to use the Pingdom REST API from Classic ASP, but the following code:-
' setup the URL
baseUrl = "https://api.pingdom.com/api/2.0/checks"
' setup the request and authorization
Set http = Server.CreateObject("MSXML2.ServerXMLHTTP")
http.open "GET", baseUrl, False
http.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
http.setRequestHeader "userpwd", "aaaaaaaaaaaaaaaaaaaaaaa:bbbbbbbbbbb"
http.setRequestHeader "App-Key", "ccccccccccccccccccccccccccccccc"
' send the HTTP data
http.send
gives me the error:-
{"error":{"statuscode":401,"statusdesc":"Unauthorized","errormessage":"User credentials missing"}}
so my authentication isnt being passed correctly, and it looks like it should NOT be passed in the requestheader, but I'm not sure how it should be done.
Thanks
Thanks for Alex K. and for the benefit of others, the correct syntax is:-
' setup the URL
baseUrl = "https://api.pingdom.com/api/2.0/checks"
Response.Write fullUrl
' setup the request and authorization
Set http = Server.CreateObject("MSXML2.ServerXMLHTTP")
http.open "GET", baseUrl, False, "emailaddress", "password"
http.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
http.setRequestHeader "App-Key", "keykeykeykeykeykeykeykeykeykey"
' send the HTTP data
http.send
:-)
https://api.pingdom.com/api/2.0/checks is using Basic Authentication so you need to pass your credentials in the .open call.

How create server side multiple request?

I am working with 3 xml's,
i wand to send my 3 xml request to different http's in same time, How it possible?
I using classic asp(VBSCRIPT), below is my code now i use.
Below code is working fine.. But the problem is to take more time to send and recive xml (because it send in different time)
<%
' xml 1
pXML=Server.URLencode(XML_REQUEST_ONE)
set xmlhttp = server.CreateObject("MSXML2.ServerXMLHTTP")
xmlhttp.open "post", servletURLH01B, false
xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
xmlhttp.send "xml_request=" &pXML
XMLresult_ONE= xmlhttp.responsexml.xml
Set xd= Server.CreateObject ("Microsoft.XMLDOM")
xd.async = "false"
xd.loadXML(XMLresult_ONE)
' xml 2
pXML=Server.URLencode(XML_REQUEST_TWO)
set xmlhttp = server.CreateObject("MSXML2.ServerXMLHTTP")
xmlhttp.open "post", servletURLH01B, false
xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
xmlhttp.send "xml_request=" &pXML
XMLresult_TWO= xmlhttp.responsexml.xml
Set xd= Server.CreateObject ("Microsoft.XMLDOM")
xd.async = "false"
xd.loadXML(XMLresult_TWO)
' xml 3
pXML=Server.URLencode(XML_REQUEST_THREE)
set xmlhttp = server.CreateObject("MSXML2.ServerXMLHTTP")
xmlhttp.open "post", servletURLH01B, false
xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
xmlhttp.send "xml_request=" &pXML
XMLresult_THREE= xmlhttp.responsexml.xml
Set xd= Server.CreateObject ("Microsoft.XMLDOM")
xd.async = "false"
xd.loadXML(XMLresult_THREE)
%>
Is it possible to send request and recive response in same time?
hoping ur support
Alex
I'm not sure what you are asking for...
it is impossible to 'send request and recive response in same time'
this is generally due to network latency. Each request over a network is delayed by the physical limitations of the speed of electricity (and server/network loads etc)
BUT i'm really not sure that this is what you are asking.
Do you want to be able to send all three requests simultaneously? and then wait for each response?

Encoding "<" and ">" while sending XML via HTTP Post

I am sending an XML content via HTTP Post from Access VBA to Web Methods, using XMLHTTP object in MSXML. Here is the Code.
Dim objXmlHttp As Object
Set objXmlHttp = CreateObject("MSXML2.ServerXMLHTTP")
objXmlHttp.Open "POST", webServicePath, False
objXmlHttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
Dim Response As String
objXmlHttp.send wrt.output
'OK status
If objXmlHttp.Status = 200 Then
Response = objXmlHttp.responseText
End If
Set objXmlHttp = Nothing
I am getting the XML with "&lt" and "&gt" instead of < and >.
If I try to do URL encoding, everything is received as ASCII text in the Recipient side.
Can you please guide what I need to do to get the valid XML format.
You need to set the content-type correctly, try this instead:
objXmlHttp.setRequestHeader "Content-Type", "text/xml; charset=""utf-8"""

Resources