Hi,
We are using the W3 validator service to check some pages on our website.
Checking a whole file with GET (passing the URL) works fine. Here is a code sample:
main
define
url string,
http_req com.HTTPRequest,
http_resp com.HTTPResponse,
doc xml.DomDocument,
validate_node xml.domNode
let http_req = com.HTTPRequest.Create("https://validator.w3.org/nu/")
let url = "http://infohorse.hrnz.co.nz/datahr/results/012557rs.htm"
call http_req.setMethod( "GET" )
call http_req.doFormEncodedRequest( sfmt("out=xml&doc=%1", url.trim()), TRUE )
let http_resp = http_req.getResponse()
if http_resp.getStatusCode() = 200 then
let doc = http_resp.getXmlResponse()
call doc.setFeature("format-pretty-print", TRUE)
let validate_node = doc.getDocumentElement()
display " debug jjt validate_node = ", validate_node.toString()
else
display " debug no good ", http_resp.getStatusCode()
end if
end main
What we would like to do in addition to checking the whole file is check an HTML string.
This looks like it requires a POST. I do not see how this works.
The validator documentation has an example using java. See:
https://github.com/validator/validator/wiki/Service:-Input:-POST-body
How can I do the equivalent of:
.queryString("out", "xml")
.body(source)
I don't see a combination of methods that allows me to pass the parameters, and the html string to check as the body.
Any help would be appreciated.
John
Hi,
The doFormEncodedRequest operation is to emulate an HTML Post submit form, in your case you only need to specify in the query string the expected response format (xml) and send the HTML page you want to validate as a file in the HTTP body request.
Try something like following and tell me if it works :
IMPORT COM
IMPORT XML
main
define
file string,
http_req com.HTTPRequest,
http_resp com.HTTPResponse,
doc xml.DomDocument,
validate_node xml.domNode
let http_req = com.HTTPRequest.Create("https://validator.w3.org/nu/?out=xml")
let file= "012557rs.html"
call http_req.setMethod( "POST" )
call http_req.doFileRequest(file)
let http_resp = http_req.getResponse()
if http_resp.getStatusCode() = 200 then
let doc = http_resp.getXmlResponse()
call doc.setFeature("format-pretty-print", TRUE)
let validate_node = doc.getDocumentElement()
display " debug jjt validate_node = ", validate_node.toString()
else
display " debug no good ", http_resp.getStatusCode()
end if
end main
Regards,
Frank
Hi Frank,
Thanks for the help. The bit I was missing was placing the expected response in the query string. We just want to check a string of HTML code from within a program. The following now works fine. Thanks for your help.
John
main
define
http_req com.HTTPRequest,
http_resp com.HTTPResponse,
str string
let http_req = com.HTTPRequest.Create("https://validator.w3.org/nu/?out=xml")
call http_req.setHeader("Content-type","text/html; charset=utf-8")
call http_req.setMethod( "POST" )
let str = "<!DOCTYPE html><html><head><title>The Title</title></head><body><div>test</div></body></html>"
call http_req.doTextRequest( str)
let http_resp = http_req.getResponse()
if http_resp.getStatusCode() = 200 then
display http_resp.getTextResponse()
else
display " debug no good ", http_resp.getStatusCode()
end if
return
end main