Xbasic

HTTP_POST Function

Syntax

Result as P = http_post(url as C [,body as B [,cookie as C [,port as N [,timeout as N [,show_before_send as L [,validate_ssl_cert as L [,SSLCipherList as C]]]]]]])

Arguments

urlCharacter

The address of the server that will receive your data. Character

bodyBinary

The parameters you wish to send. The size of this field is unlimited.

cookieCharacter

Default = "". Cookie data.

portNumeric

Default = -1. The port to use. When set to -1 or 0, the correct standard port for the protocol specified will be used (e.g. 80 for http://, 443 for https://).

timeoutNumeric

Default = 8000 milliseconds. The number of milliseconds to wait before timing out.

show_before_sendLogical

Default = .F.. When .T., displays the request before being sent. Useful for debugging.

validate_ssl_certLogical

Default = .T.. If the specified URL starts with "https://", this flag controls whether or not the certificate offered by the server will be validated.

SSLCipherListCharacter

Default = "". SSL Cipher List. When an empty list is provided, the function [httpd_defaultCipherList()] is used to set the acceptable ciphers for this request.

followRedirectsLogical

Default = .F.. By default, this function will not follow any server redirects. Set this to .T. to follow redirects. The maximum allowed number of redirects is 20.

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

Use the HTTP method POST to retrieve the specified URL

Discussion

The HTTP_POST() function downloads from URL using the POST method with TLS 1.2 or newer. The function returns a pointer with the parsed response from the server. If error_code is "0", parsed_response will contain the headers in the result:

dim url as c = "https://jsonplaceholder.typicode.com/posts"
dim body as p
body.title = "This is an example"
body.body = "It contains some text"
body.userId = 1

dim req_body as c = json_generate(body)

result = http_post(url, req_body)
? result.error_code
= 0

? result.parsed_headers
= AccessControlAllowCredentials = "true"
AccessControlExposeHeaders = "Location"
altsvc = h3-27=":443"; ma=86400, h3-28=":443"; ma=86400, h3-29=":443"; ma=86400, h3=":443"; ma=86400
CacheControl = "no-cache"
CFCacheStatus = "DYNAMIC"
CFRAY = "667112ddad3c17ed-EWR"
cfrequestid = "0afa8a1e8b000017ed529ce000000001"
Connection = "close"
ContentLength = "132"
ContentType = "application/json; charset=utf-8"
Date = "Tue, 29 Jun 2021 18:03:48 GMT"
Etag = W/"84-xkcUSamvZej8jitZ3eZT7zryuxs"
ExpectCT = max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
Expires = "-1"
http_version = "HTTP/1.1"
Location = "http://jsonplaceholder.typicode.com/posts/101"
NEL = {"report_to":"cf-nel","max_age":604800}
Pragma = "no-cache"
reason_phrase = "Created"
ReportTo = {"endpoints":[{"url":"https:\/\/a.nel.cloudflare.com\/report\/v2?s=%2BQzj1CipvYUEOIi9werXqGq6kezX8jVgw6PN6cQvqH6qZogYk8dr5%2BOmFYkPgyzTjcUpj%2B9%2F3c4pA9f2yQZ1LRl4UdYG6n8tdkffdCpthF0FH9Eqm%2B1ESDIER6%2BOa2Z24vxEt2rK7qotSA%3D%3D"}],"group":"cf-nel","max_age":604800}
Server = "cloudflare"
status_code = 201
Vary = "Origin, X-HTTP-Method-Override, Accept-Encoding"
Via = "1.1 vegur"
XContentTypeOptions = "nosniff"
XPoweredBy = "Express"
XRatelimitLimit = "1000"
XRatelimitRemaining = "999"
XRatelimitReset = "1624989871"
HTTP_POST sends using a content type of "Content-Type: application/x-www-form-urlencoded". To send a request using a different content type, use HTTP_FETCH().

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 301, 302, 307, or 308 code, HTTP_GET() does not automatically use the new URL. As of build 8596, developers my optionally specify that redirects should be followed by using the settings.Redirects.follow property. In earlier builds or if automatice redirect handling is not desired, 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.

HTTP POST vs HTTP GET

HTTP POST is different from HTTP GET in two significant ways. Some devices may restrict the total length of a URL to 128 characters, which may trim the arguments appended by a GET command. There is no such restriction with POST commands. You may save (bookmark) and refresh URLs formatted through HTTP GET. The arguments sent by HTTP POST are not saved in a bookmark.

See Also