Xbasic

HTTP_FETCH Function

Syntax

Result as P = http_fetch(settings as P [,show_before_send as L [,SSLCipherList as C]])

Arguments

settingsPointer

A dot variable containing instructions for the command. Pointer

hostCharacter

The host (server) from which to download.

Do not include the protocol (HTTP:// or HTTPS://) in the value
portNumeric

Default = 80 for HTTP (.ssl_on=.f.) and 443 for HTTPS (.ssl_on=.t.). The TCP/IP port on the server with which to communicate.

pageCharacter

Default = "/". The page to request from the server.

query_stringCharacter

The query string for the request.

methodCharacter

Default = "GET". The HTTP method to use when making the request (GET, POST, HEAD, OPTIONS, etc).

protocolCharacter

Default = "HTTP/1.1". The protocol to use for the request.

user_agentCharacter

Default = "". Sets the User-agent header in the request.

cookieCharacter

Default = "". Sets the value of the Cookie header.

bodyCharacter

Default = "". The contents of the HTTP request body.

refererCharacter

Default = "". Sets the Referer header in the request.

timeoutNumeric

Default = 45000 (45 seconds). Sets the timeout value, in milliseconds, for the connection.

headerCharacter

Default = "". Allows setting of arbitrary headers. Would be used as:  settings.header = "X-Header: I like Alpha Software"

SSL_ONLogical

Default = .F.

ssl_ValidateCert

Default = .T.. If the specified URL starts with "https://", this flag controls whether or not the certificate offered by the server will be validated. If set to .T., the request will always fail if the certificate cannot be validated. If set to .F., the request may succeed, but the server's identity is not guaranteed.

show_before_sendLogical

Default = .F.. When .T., displays the request before being sent. Useful for debugging. Show_Before_Send is ignored if http_fetch is run from an A5W page.

SSLCipherListCharacter

SSL Cipher List

Returns

ResultPointer

A dot variable containing the server's response.

error_textCharacter

The error message, if any. If no error occurs, error_text will be empty.

error_codeNumeric

The error number, if any. If no error occurs, error_code will be 0.

headersCharacter

Response headers. If error_code is 0, the result will contain a dot variable, parsed_headers, with a property representing each response header.

bodyCharacter

Response body.

parsed_headersPointer

Contains all the headers in the response split out into individual properties. If an error occurred when trying to communicate with the server, parsed_headers will be empty. parsed_headers will always contain the following properties in addition to the headers in the response:

http_versionCharacter

The HTTP version used.

reason_phraseCharacter

A description of the status code.

status_codeNumeric

The response status code. See Status Codes for a list of status codes.

Description

Sends an HTTP request to a server and retrieves the response

Discussion

The HTTP_FETCH() function sends an HTTP request to a server and receives the response. The function looks to see if a request body has been defined first. If one is found, and no .method has been specified, it uses POST, otherwise it uses GET. If a .method has been specified, that method will always be used, even if it is not a valid HTTP method. Similarly, if a .body is specified and the .method is not POST or PUT, the body is not sent.

If error_code is "0", parsed_response will contain the headers in the result. The parsed headers will contain every header sent in the response, as well as the status_code, reason_phrase, and http_version. The status_code should always be checked when the response is received. The example below shows what headers may be available in the result:

dim settings as P

dim settings.host as C = "www.alphasoftware.com"
dim settings.page as C = "/"

dim result as P
result = http_fetch(settings)

? result.error_code
= 0

? result.parsed_headers
= AcceptRanges = "none"
CacheControl = "private"
Connection = "close"
ContentLength = "47222"
ContentType = "text/html"
Date = "Thu, 22 Dec 2016 22:11:23 GMT"
http_version = "HTTP/1.1"
reason_phrase = "OK"
Server = "Microsoft-IIS/8.5"
SetCookie = "ASPSESSIONIDQSTBASCQ=KGOAFDNBPPOBEOIJOFEDADFA; path=/"
status_code = 200
XPoweredBy = "ASP.NET"

Status code 200 indicates that the page exists. Status code 404 indicates that it does not. Other status codes you may encounter, along with the meanings of each, are documented in Status Codes.

 Handling Redirects

When the server responds with a 30* code, HTTP_POST() does not automatically use the new URL. The developer needs to examine result.parsed_headers.status_code, then if appropriate, try the URL provided in result.parsed_headers.location. Refer to HTTP_GET() for an example.

 Example: Page Availability

The following example tests for the availability of a page:

dim settings as P
dim result as P

settings.host = "downloads.alphasoftware.com"
settings.page = "/books/userguide.pdf"
settings.method = "HEAD"
result = http_fetch(settings)

if (result.error_code <> 0) then
    ui_msg_box("Error communicating with server. Error code " + result.error_code + ": " + result.error_text)
else
    if (result.parsed_headers.status_code = 200) then
        a5_show_html(result.body)
    else
        ui_msg_box(result.parsed_headers.status_code + ": " + result.parsed_headers.reason_phrase)
    end if
end if

 Validation

Validation is applied to the host specified for the HTTP request. If a full URL has been specified as the host name, the validation logic will extract the host name from the URL, allowing the HTTP request to be completed.

See Also