My REST API returns a PDF document in bytes and I need to call that API and show the PDF document on the ASP page for previewing to the user.
I tried
Response.Write HttpReq.responseBody
but it's writing some unreadable text on the page. The httpReq is my object through which I am calling the REST API.
Response of REST API:
Request.CreateResponse(HttpStatusCode.OK, pdfStream, MediaTypeHeaderValue.Parse("application/pdf"))
You'll have to define the content type of the response as PDF:
Response.ContentType = "application/pdf"
Then write the binary data to the response:
Response.BinaryWrite(httpReq.ResponseBody)
Full example:
url = "http://yourURL"
Set httpReq = Server.CreateObject("MSXML2.ServerXMLHTTP")
httpReq.Open "GET", url, False
httpReq.Send
If httpReq.Status = "200" Then
Response.ContentType = "application/pdf"
Response.BinaryWrite(httpReq.ResponseBody)
Else
' Display an error message
Response.Write("Error")
End If
In Classic ASP, Response.Write() is used to send textual data back to the browser using the CodePage and Charset properties defined on the Response object (by default this is inherited from the current Session and by extension the IIS Server Configuration).
To send binary data back to the browser use Response.BinaryWrite().
Here is a quick example (snippet based off you already having the binary from httpReq.ResponseBody);
<%
Response.ContentType = "application/pdf"
'Make sure nothing in the Response buffer.
Call Response.Clear()
'Force the browser to display instead of bringing up the download dialog.
Call Response.AddHeader("Content-Disposition", "inline;filename=somepdf.pdf")
'Write binary from the xhr responses body.
Call Response.BinaryWrite(httpReq.ResponseBody)
%>
Ideally, when using a REST API via an XHR (or any URL for that matter) you should be checking the httpReq.Status to allow you to handle any errors separately to returning the binary, even set a different content-type if there is an error.
You could restructure the above example;
<%
'Make sure nothing in the Response buffer.
Call Response.Clear()
'Check we have a valid status returned from the XHR.
If httpReq.Status = 200 Then
Response.ContentType = "application/pdf"
'Force the browser to display instead of bringing up the download dialog.
Call Response.AddHeader("Content-Disposition", "inline;filename=somepdf.pdf")
'Write binary from the xhr responses body.
Call Response.BinaryWrite(httpReq.ResponseBody)
Else
'Set Content-Type to HTML and return a relevant error message.
Response.ContentType = "text/html"
'...
End If
%>
Content-Disposition:What are the differences between “inline” and “attachment”?
A: ASP Classic, Download big files does not work on certain servers (useful tips on chunking your downloads)
Related
I have been using MSXML2.XMLHTTP.6.0 successfully with my Excel VBA script for a couple years, for both GET and POST requests. The POST request looks like:
Dim zipService as Object
Dim Query As String
Dim Body As String
Set zipService = CreateObject("MSXML2.XMLHTTP.6.0")
Query = "https://10.xxx.xxx.xxx:443/api/rest/v1/bulk/thresholdRules/"
zipService.Open "POST", Query, False
zipService.setRequestHeader "Authorization", "Basic " & gsUserIDPW
zipService.setRequestHeader "Content-Type", "application/json"
zipService.setRequestHeader "Accept", "application/xml"
Body = "Body of the request"
zipService.send (sBody)
Now I need to need to make a PATCH request to a new REST call for the same API. I have tested the PATCH REST call in Postman, and it works successfully. Using PUT in Postman also works. However, in Excel, when I try to do the Open call using PATCH (or PUT), the Open call fails with a -2147012891 error.
Any suggestions? Are PATCH and PUT allowed with the Open method? Are there other parameters I have to specify? All the code examples I find online are always GET and POST requests.
I'm a totally beginner with webrequest, so I have no idea about what cause the error I get.
I try to login on a form following the microsoft tutorial for webrequest, but when I want to get the server response, I have the following error :
"the remote server returned an error (404) not found"
So I know that the URL I use actually exist and then wonder which part of the code is bad. Maybe it's because I'm doing an HTTPS request unlike the tutorial and it changes something ?
Also, I'm a little confused by getting directly the answer from the server : shouldn't there be kind of a trigger to know when the server answered ?
Dim request = WebRequest.Create("https://ssl.vocabell.com/mytica2/login")
request.Credentials = CredentialCache.DefaultCredentials
request.Method = "POST"
Dim byteArray = Encoding.UTF8.GetBytes("_username=x&_password=x")
request.ContentType = "application/x-www-form-urlencoded"
request.ContentLength = byteArray.Length
Dim dataStream = request.GetRequestStream()
dataStream.Write(byteArray, 0, byteArray.Length)
dataStream.Close()
Dim reponse = request.GetResponse() 'ERROR
MsgBox(CType(reponse, HttpWebResponse).StatusDescription)
Using ds = reponse.GetResponseStream
Dim reader = New StreamReader(ds)
MsgBox(reader.ReadToEnd)
End Using
reponse.Close()
Thank you for your time, and if you have any relevant tutorial on the topic I would be glad to read it !
The page you've mentioned does exist and uses HTTPS, but if you look at the form tag within it, it's like this:
<form class="login-form form-horizontal" action="/mytica2/login_check" method="POST">
This means it doesn't post the form back to the same URL as the page, instead it sends it to the URL contained within that "action" attribute. If you're trying to use your code to simulate the submission of the login form then it looks like you need to send your POST request to https://ssl.vocabell.com/mytica2/login_check instead.
I’m writing a http module to work as a reverse proxy, i.e. receives a request from a browser, sends it on to the target site, receives a response and sends that back to the browser.
Its all working fine, except for a problem with forwarding cookies from the browser request to the target site on a Post. All headers and form data are correct on the outgoing request, but no cookies are included.
I’ve run fiddler on both the request from the browser to IIS and the outgoing httpwebrequest and proven this to be the case. Running the module in debug shows that the cookies are found in the request from the browser and successfully placed in the cookiecontainer of the httpwebrequest, but they just don’t appear in the actual request sent out.
If I hack (in debug) the outgoing request method to a Get, then they go, but they don’t go for a Post.
I’ve also tracked the request/response from a browser direct to the target site using Fiddler, and the request seems identical in all three cases (browser to target, browser to my IIS module, IIS module to target), except that the IIS module to target omits the cookies.
Here’s the code (VB.Net, and tried in 2.0 and 4.5):
' set up the request to the target
Dim reqTarget As System.Net.HttpWebRequest
reqTarget = CType(System.Net.HttpWebRequest.Create(strTargetURL & strTargetPath & qstring), System.Net.HttpWebRequest)
' copy relevant info, cookies etc from the application request to the target request
CopyAppRequest(application.Context.Request, reqTarget)
' send the request and get the response
Dim rspTarget As System.Net.HttpWebResponse = CType(reqTarget.GetResponse(), System.Net.HttpWebResponse)
Private Sub CopyAppRequest(ByRef reqApp As System.Web.HttpRequest, ByRef reqTarget As System.Net.HttpWebRequest)
' copy over the headers
For Each key As String In reqApp.Headers.AllKeys
Select Case key
Case "Host", "Connection", "Content-Length", "Accept-Encoding", "Expect", "Authorization", "If-Modified-Since"
' not sure if we need to process these
Case "Connection"
reqTarget.Connection = reqApp.Headers(key)
Case "Content-Type"
reqTarget.ContentType = reqApp.Headers(key)
Case "Accept"
reqTarget.Accept = reqApp.Headers(key)
Case "Referer"
reqTarget.Referer = reqApp.Headers(key)
Case "User-Agent"
reqTarget.UserAgent = reqApp.Headers(key)
Case "Cookie"
' do nothing, cookies are handled below..
Case Else
reqTarget.Headers.Add(key, reqApp.Headers(key)
End Select
Next
reqTarget.Method = reqApp.HttpMethod
reqTarget.AllowAutoRedirect = False
If reqTarget.Method = "POST" Then
reqTarget.ContentLength = reqApp.ContentLength
Dim datastream() As Byte = System.Text.Encoding.UTF8.GetBytes(reqApp.Form.ToString)
reqTarget.ContentLength = datastream.Length
Dim requestwriter As System.IO.Stream = reqTarget.GetRequestStream
requestwriter.Write(datastream, 0, datastream.Length)
requestwriter.Close()
requestwriter.Dispose()
End If
Dim CookieJar As New System.Net.CookieContainer
reqTarget.CookieContainer = CookieJar
For Each key As String In reqApp.Cookies.AllKeys
Dim tgtCookie As New System.Net.Cookie
With tgtCookie
.Name = reqApp.Cookies.Item(key).Name
.Value = reqApp.Cookies.Item(key).Value
.Domain = ".domain.com"
.Path = "/"
.Expires = DateAdd(DateInterval.Month, 1, System.DateTime.Now)
.HttpOnly = True
End With
CookieJar.Add(tgtCookie)
Next
End Sub
Note: the domain I’m trying to reach is in the form abc.domain.com (i.e. it’s a subdomain, and no www), the reason I’ve tried the .domain.com form is that is the form used in the cookies that are received in the response. I’ve also tried other combinations such as abc.domain.com, .abc.domain.com, etc. Also I’ve tried creating a Uri object and using that method to add the cookie into the cookiecontainer.
I’ve tried everything I can think of and can find on forums…. Anyone got any suggestions? I suspect I’ve missed something obvious!
Of course, any other comments on how the code above can be improved will be appreciated.
Thanks.
Ok, I found the issue...
I was using Fiddler to see what was going on with http, and Fiddler was correct. However, when I used Wireshark, I found that the http request was being sent earlier than I thought, and only on the Post.
It turned out that the requestwriter.write line caused the http request to be sent, not the GetResponse (as is the case with Get). So, anything I changed in the httpwebrequest after the requestwriter.write didn't get sent.
The fix - I just moved all the header and cookie set up above the requestwriter.write and it all worked.
How frustrating, but at least its fixed now :)
If anyone has any feedback on whether I've got something wrong that's causing this to happen, please let me know.
I have a current web site that captures data send via querystring
I would like to repost this data to a different API using HTTP GET and capture the response from that site. Response.Redirect works but can not read the results of that post.
This sample code should get you started. It requests the page from a url with querystring values passed, and stores the response in a variable then response.write's the contents.
Set objXML = Server.CreateObject("MSXML2.ServerXMLHTTP")
objXML.Open "GET", "http://www.test.com/test.asp?var1=val1", false
objXML.Send ""
strResponse = objXML.ResponseText
Response.Write strResponse
I'm trying to send a findItemsAdvanced request to the eBay API from VB 2012 .NET - however each time I get an "internal server error" with errorcode "500" upon request.GetResponse() where request = System.Net.WebRequest.Create("http://svcs.ebay.com/services/search/FindingService/v1"). I do the request via POST and in XML format with the lines
Dim Xml_bytes() As Byte = System.Text.Encoding.UTF8.GetBytes(xmlstr)
request = System.Net.WebRequest.Create("http://svcs.ebay.com/services/search/FindingService/v1")
'... code setting HEADERS, see below
request.Method = "POST"
request.ContentLength = Xml_bytes.Length
request.ContentType = "text/xml"
Dim requestStream As System.IO.Stream = request.GetRequestStream()
requestStream.Write(Xml_bytes, 0, Xml_bytes.Length)
requestStream.Close()
The code setting the headers I left out, instead here the actual finished headers I supply:
X-EBAY-SOA-SERVICE-NAME: FindingService
X-EBAY-SOA-OPERATION-NAME: findItemsAdvanced
X-EBAY-SOA-SERVICE-VERSION: 1.0.0
X-EBAY-SOA-GLOBAL-ID: EBAY-DE
X-EBAY-SOA-SECURITY-APPNAME: ****
X-EBAY-SOA-REQUEST-DATA-FORMAT: XML
Content-Type: text/xml
And finally the actual XML code which is supplied (through xmlstr var above):
<?xml version='1.0' encoding='utf-8'?>
<findItemsAdancedRequest xmlns='http://www.ebay.com/marketplace/search/v1/services'>
<keywords>caddy</keywords>
<categoryId>77994</categoryId>
<Version>779</Version>
<paginationInput><entriesPerPage>10</entriesPerPage>
</paginationInput>
</findItemsAdvancedRequest>
My main problem is: it does work perfectly with findItemsByKeywords! With the exact same code, only changing the find method and leaving out the <categoryId>77994</categoryId>.
What I tried to do:
different sites (US, german,..)
different categories and keywords.
live AND sandbox APIs with the correct appIDs
including or excluding X-EBAY-SOA-RESPONSE-DATA-FORMAT and/or
X-EBAY-SOA-REQUEST-DATA-FORMAT from the header
It's always the same: it works fine with findItemsByKeyword but I get the "internal server error" with findItemsAdvanced
I have run out of ideas - any suggestions?
Thanks!