Ns http

From AOLserver Wiki
Jump to navigation Jump to search

<manpage>ns_http</manpage>

NAME

ns_http - Simple HTTP client functionality

SYNOPSIS

ns_http option ?arg arg ...?

DESCRIPTION

This command provides a simple HTTP client mechanism.
The legal options (which may be abbreviated) are:
  • ns_http cancel id
  • ns_http cleanup
  • ns_http queue method url ?body? ?headers?
body is the value which will be sent as the HTTP request body. headers is the ns_set ID containing the additional headers to include in the HTTP request.
  • ns_http wait id resultsVar ?timeout? ?headers? ?-servicetime svcTime?
resultsVar is the name of a variable that should be used to store the HTTP response body. Default timeout is "2:0" (2s, 0usec). headers is the ns_set ID which will receive the headers from the HTTP response.

Please note that in AOLserver 4.5 the API for ns_http has been enhanced, please see the section USAGE IN AOLSERVER 4.5 below.

EXAMPLES

Valid HTTP GET:

  % set id [ns_http queue GET http://aolserver.com/]
  http0
  % set status [ns_http wait $id results]
  1
  % string range $results 0 60
  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN

Timed out HTTP GET:

  % set id [ns_http queue GET http://aolserver.com:12345/]
  http0
  % set status [ns_http wait $id results 0]
  0
  % set results
  timeout

Bogus HTTP GET:

  % catch {ns_http queue GET http://nonexistant.domain/} err
  1
  % set err
  could not connect to : http://nonexistant.domain/

USAGE IN AOLSERVER 4.5

In AOLserver 4.5 the ns_http command has been enhanced. Currently only ns_http queue and ns_http wait are documented. If you have documentation for ns_http cancel and ns_http cleanup please add it to this section.

DESCRIPTION

timeout is the timeout for the request, method is the method for the request, body is the body to be sent with the request and headers is an ns_set containing headers to be sent with the request.
  • ns_http queue ?-timeout timeout? ?-method method? ?-body body? ?-headers headers? url
resultVar is the variable into which the body of the response is to be set, elapsedVar is a variable into which the elapsed time for the request is set, headersVar is an ns_set into which the response headers are set and statusVar is a variable into which the status line of the response is set.
  • ns_http wait -result resultVar ?-elapsed elapsedVar? ?-headers headersVar? ?-status statusVar? id

EXAMPLE

  set timeout 30 
  set method GET
  set body ""
  set outputheaders [ns_set create outputheaders]
  set headersVar [ns_set create headersVar]
  set url http://www.aolserver.com/

  set id [ns_http queue -timeout $timeout -method $method -body $body -headers $outputheaders $url]

  set status [ns_http wait -elapsed elapsedVar -result resultVar -headers $headersVar -status statusVar $id]

SEE ALSO

ns_httpopen