Xbasic

How to pick an HTTP Client Function

Description

There are two groups of HTTP functions available in the Xbasic language - those implemented purely in Xbasic and those that rely on a Microsoft object that is included with the Windows operating system.

In general, the Microsoft-based functions are useful in a desktop application where it is known that there is a proxy server in use or the application may have wide distribution and needs to be able to handle proxy servers. If you do not need to worry about proxy servers, the pure Xbasic functions provide additional functionality which is often compelling.

Pure Xbasic Implementation

This group of functions uses a pure Xbasic socket implementation to initiate the connection, build and send the request, and receive the response. These functions handle all of the HTTP protocol work, but by design they do not automatically handle cookies or redirects. If cookies or following redirects are required in a specific application, additional code must be written by the developer to extend the built-in functionality.

HTTP_FETCH() provides the underlying functionality for all of the other functions. As a result, it provides more options and flexibility but requires a bit more code to work with. In most cases, the functions specific to the HTTP method are sufficient and easier to work with. The most common reason to use HTTP_FETCH() is when a developer needs to set a custom HTTP header as part of the request.

HTTP_DOWNLOAD() and HTTP_DOWNLOAD_BG() do not support https. Use HTTP_FETCH() instead.

If no error occurs when attempting to contact the server, these functions return an object containing the parsed response headers. Check the value of result.parsed_headers.error_code verify whether or not the request succeeded. A value of 200 indicates success. A value of 404 indicates the page could not be found. See Status Codes for a list of status codes and their meaning.

The pure Xbasic functions do not automatically handle 301, 302, 307, or 308 redirects. It is up to the developer to check the status code and perform the redirect, if necessary. See HTTP_GET() for an example of handling a redirect.

XMLHttpRequest Implementation

This group of functions uses a Microsoft object (XMLHttpRequest) to make the request and receive the response. As result, the developer gives up some flexibility, but gains automatic cookie handling and redirection, as well as automatic inheritance of the proxy settings defined in Windows' Internet Options. These may or may not be benefits, depending on the exact application.

These functions based on XMLHttpRequest are not server-safe and cannot be used in an A5W environment. If you attempt to use them there, they will be automatically remapped to the pure Xbasic implementation. Rather than rely on this automatic remapping and possible confusion when debugging, it is strongly recommended to use the Xbasic implementation functions directly in all A5W code.

See Also