Jagadeesh
Jagadeesh

Reputation: 1790

In Classic ASP, Getting request from Server(API) in XML Formate

I need to take request from server url. Is that in XML format or not.

Set objServer = Server.CreateObject("MSXML2.ServerXMLHTTP.3.0")
    Set objXML       = Server.CreateObject("Msxml2.DOMDocument.3.0")

objerver.setTimeouts 30000, 30000, 30000, 30000
    objServer.Open "GET", "https://api.myurl/", False        
    objServer.setRequestHeader "Content-Type", "application/xml; charset=utf-8"
    objServer.setRequestHeader "Accept", "application/xml"

ObjServer.Send 

strXMLResponse = objServer.responseText
    blnXMLLoaded = objXML.loadXML(objServer.responseText)

If Not blnXMLLoaded Then
    aryReturn(0) = "XML failed to Load"
Else
    aryReturn(0) = "succssResult"
End If

When I attempt to run this experiment (with the broken/wrong URL) what I am getting is a run-time error. Because XMLLoaded to TRUE, but I want it to set it to FALSE. It is receiving back an HTML document, But here HTML also have some XML tags. so, though it is XML and the XMLLoaded flag is set to true, and so the error never gets thrown

Can any one help me please. Thanks, Jagadi

Upvotes: 0

Views: 2765

Answers (1)

AnthonyWJones
AnthonyWJones

Reputation: 189457

Your problem is that you have ignored the response status from the http request. You should at least be sure to test the status property is 200 before using responseText:

blnXMLLoaded = false
If objServer.status = 200 Then
    blnXMLLoaded = objXML.loadXML(objServer.responseText)
End If

The whole sequence I would actually code as:

Function GetXML(url)
    Dim xhr: Set xhr = Server.CreateObject("MSXML2.ServerXMLHTTP.3.0")
    xhr.setTimeouts 30000, 30000, 30000, 30000   
    xhr.Open "GET", url, False
    If xhr.status = 200 Then
        Set GetXML = xhr.responseXML
    Else
        Set GetXML = Nothing
    End If
End Function

Dim objXML : Set objXML = GetXML("https://api.myurl/")
If Not objXML Is Nothing Then
    ''# All is good stuff here
Else
    ''# Oops something bad happend stuff here
End If

Upvotes: 1

Related Questions