4D v16.3

HTTP Get

Home

 
4D v16.3
HTTP Get

HTTP Get 


 

HTTP Get ( url ; response {; headerNames ; headerValues}{; *} ) -> Function result 
Parameter Type   Description
url  Text in URL to which to send the request
response  Text, BLOB, Picture, Object in Result of request
headerNames  Text array in Header names of the request
in Returned header names
headerValues  Text array in Header values of the request
in Returned header values
Operator in If passed, connection is maintained (keep-alive)
If omitted, connection is closed automatically
Function result  Longint in HTTP status code

The HTTP Get command sends an HTTP GET request directly to a specific URL and processes the HTTP server response.

Pass the URL where you want the request sent in the url parameter. The syntax to use is:

http://[{user}:[{password}]@]host[:{port}][/{path}][?{queryString}]

For example, you can pass the following strings:

    http://www.myserver.com
    http://www.myserver.com/path
    http://www.myserver.com/path?name="jones"
    https://www.myserver.com/login (*)
    http://123.45.67.89:8083
    http://john:smith@123.45.67.89:8083

(*) During HTTPS requests, authority of the certificate is not checked.

After command execution, the response parameter receives the result of the request returned by the server. This result corresponds to the body of the response, with no headers.
You can pass different types of variables in response:

  • Text: When the result is expected to be text (see note below).
  • BLOB: When the result is expected to be in binary form. 
  • Picture: When the result is expected to be a picture.
  • Object: When the result is expected to be a C_OBJECT object.

Note: When a text variable is passed in response, 4D will try to decode the data returned from the server. 4D first tries to retrieve the charset from the content-type header, then from the content using a BOM, and finally looks for any http-equiv charset (in html content) or encoding (for xml) attribute. If no charset can be detected, 4D will attempt to decode the response in ANSI. If the conversion fails, the resulting text will be empty. If you are unsure whether the server returns a charset information or a BOM, but you know the encoding, it is more accurate to pass response in BLOB and call Convert to text.

If you pass a BLOB, it contains the text, picture or any type of contents (.wav, .zip, etc.) returned by the server. You must then manage the recovery of these contents (headers are not included in the BLOB). When you pass a C_OBJECT type object, if the request returns a result with an "application/json" (or "something/json") content-type, 4D attempts to parse the JSON content in order to generate the object.

In headerNames and headerValues, you pass arrays containing the names and values of the request headers.
After this method is executed, these arrays contain the names and values of headers returned by the HTTP server. More specifically, this lets you manage cookies. 

The * parameter enables the keep-alive mechanism for the server connection. By default, if this parameter is omitted, keep-alive is not enabled. 

The command returns a standard HTTP status code (200=OK and so on) as returned by the server. The list of HTTP status codes is provided in RFC 2616.
If you are unable to connect to the server for a reason related to the network (DNS Failed, Server not reachable...), the command returns 0 and an error is generated. You can intercept errors using an error-handling method installed by the ON ERR CALL command.

Retrieval of the 4D logo on the 4D Web site:

 C_TEXT(URLPic_t)
 URLPic_t:="http://www.4d.com/sites/all/themes/dimention/images/home/logo4D.jpg"
 ARRAY TEXT(HeaderNames_at;0)
 ARRAY TEXT(HeaderValues_at;0)
 C_PICTURE(Pic_i)
 $httpResponse:=HTTP Get(URLPic_t;Pic_i;HeaderNames_at;HeaderValues_at)

Retrieval of an RFC:

 C_TEXT(URLText_t)
 C_TEXT(Text_t)
 URLText_t:="http://tools.ietf.org/rfc/rfc1.txt"
 ARRAY TEXT(HeaderNames_at;0)
 ARRAY TEXT(HeaderValues_at;0)
 $httpResponse:=HTTP Get(URLText_t;Text_t;HeaderNames_at;HeaderValues_at)

Retrieval of a video:

 C_BLOB(vBlob)
 $httpResponse:=HTTP Get("http://www.example.com/video.flv";vBlob)
 BLOB TO DOCUMENT("video.flv";vBlob)



See also 

HTTP Request

 
PROPERTIES 

Product: 4D
Theme: HTTP Client
Number: 1157

This command modifies the Error system variableThis command can be run in preemptive processes

 
HISTORY 

Created: 4D v13
Modified: 4D v14

 
ARTICLE USAGE

4D Language Reference ( 4D v16)
4D Language Reference ( 4D v16.1)
4D Language Reference ( 4D v16.2)
4D Language Reference ( 4D v16.3)