Four Js Development Tools Forum

Discussions by product => Genero BDL => Topic started by: John T. on January 29, 2016, 04:24:55 am



Title: W3 Validator service with POST
Post by: John T. on January 29, 2016, 04:24:55 am
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


Title: Re: W3 Validator service with POST
Post by: Frank G. on January 29, 2016, 09:26:09 am
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


Title: Re: W3 Validator service with POST
Post by: John T. on February 01, 2016, 12:16:05 am
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