1. HTTP Functions

  1. HTTP Functions
    1. Introduction
    2. Request Functions
      1. HTTP_GET
      2. HTTP_POST
      3. HTTP_PUT
      4. HTTP_PATCH
      5. HTTP_DELETE
    3. Authentication
      1. HTTPBASICAUTH
    4. URL Encoding
      1. URL_ENCODE
      2. URL_DECODE
    5. Status Codes
    6. Response Headers
    7. Cookies
    8. Headers
    9. JSON API Example
    10. XML API Example
    11. Notes

1.1. Introduction

The ACF language includes built-in HTTP functions for calling web services and APIs directly from ACF code. They can be used for REST APIs, JSON APIs, XML/SOAP endpoints, form posts, synchronization tasks, and other integrations where a FileMaker solution needs to send or retrieve data over HTTP or HTTPS.

The main request functions return the response body as a string. After each request, the system variable HTTP_STATUS_CODE contains the HTTP status code returned by the server, such as 200, 201, 204, 400, 401, or 500. The system variable HTTP_HEADERS contains the response headers from the last HTTP request.

Headers are supplied as a string with one header per line. Use carriage return (\r) between header lines.

1.2. Request Functions

1.2.1. HTTP_GET

Example:

function GetCustomerFromAPI(string customerId)
    string url = "https://api.example.com/customers/" + URL_ENCODE(customerId);
    string headers = "Accept: application/json";
    string response = HTTP_GET(url, headers);

    if (HTTP_STATUS_CODE != 200) then
        throw format("HTTP GET failed. Status=%d Response=%s", HTTP_STATUS_CODE, response);
    end if

    return response;
end

1.2.2. HTTP_POST

Example:

function CreateRemoteCustomer(JSON customer)
    string url = "https://api.example.com/customers";
    string headers = "Content-Type: application/json\rAccept: application/json";
    string response = HTTP_POST(url, string(customer), headers);

    if (HTTP_STATUS_CODE < 200 || HTTP_STATUS_CODE >= 300) then
        throw format("HTTP POST failed. Status=%d Response=%s", HTTP_STATUS_CODE, response);
    end if

    return response;
end

1.2.3. HTTP_PUT

Example:

string response = HTTP_PUT(
    "https://api.example.com/customers/123",
    string(customerJson),
    "Content-Type: application/json\rAccept: application/json"
);

1.2.4. HTTP_PATCH

Example:

JSON patch = JSON("status", "closed");
string response = HTTP_PATCH(
    "https://api.example.com/cases/445",
    string(patch),
    "Content-Type: application/json\rAccept: application/json"
);

1.2.5. HTTP_DELETE

Example:

string response = HTTP_DELETE(
    "https://api.example.com/customers/123",
    "",
    "Accept: application/json"
);

if (HTTP_STATUS_CODE != 200 && HTTP_STATUS_CODE != 204) then
    throw format("HTTP DELETE failed. Status=%d Response=%s", HTTP_STATUS_CODE, response);
end if

1.3. Authentication

1.3.1. HTTPBASICAUTH

Example:

string response = HTTP_GET(
    "https://api.example.com/private/status",
    "Accept: application/json",
    HTTP_BASIC_AUTH("apiuser", "secret")
);

You can also include authentication directly in the regular header string if the API uses another method, such as Bearer tokens:

string headers =
    "Accept: application/json\r" +
    "Authorization: Bearer " + accessToken;

string response = HTTP_GET("https://api.example.com/me", headers);

1.4. URL Encoding

1.4.1. URL_ENCODE

Example:

string searchText = "ACF Plugin PDF & JSON";
string url = "https://api.example.com/search?q=" + URL_ENCODE(searchText);
string response = HTTP_GET(url, "Accept: application/json");

1.4.2. URL_DECODE

Example:

string normalText = URL_DECODE("ACF%20Plugin%20PDF%20%26%20JSON");

1.5. Status Codes

After any HTTP request, inspect HTTP_STATUS_CODE to decide whether the request succeeded.

Common success codes:

Code Meaning
200 OK
201 Created
202 Accepted
204 No content

Common error codes:

Code Meaning
400 Bad request
401 Unauthorized
403 Forbidden
404 Not found
409 Conflict
429 Too many requests
500 Server error

Typical check:

string response = HTTP_GET(url, headers);
if (HTTP_STATUS_CODE < 200 || HTTP_STATUS_CODE >= 300) then
    throw format("HTTP request failed. Status=%d Response=%s", HTTP_STATUS_CODE, response);
end if

1.6. Response Headers

After any HTTP request, HTTP_HEADERS contains the response headers from the last request as text.

Example:

string response = HTTP_GET(url, headers);
string responseHeaders = HTTP_HEADERS;

if (HTTP_STATUS_CODE == 200) then
    print responseHeaders;
end if

1.7. Cookies

The HTTP functions do not keep an automatic cookie jar. Cookies returned by the server are available in HTTP_HEADERS as ordinary response headers. To send a cookie on a later request, add a Cookie: line to the request header string.

Simple example with one session cookie:

string loginResponse = HTTP_POST(
    "https://api.example.com/login",
    string(loginJson),
    "Content-Type: application/json\rAccept: application/json"
);

if (HTTP_STATUS_CODE != 200) then
    throw format("Login failed. Status=%d Response=%s", HTTP_STATUS_CODE, loginResponse);
end if

string responseHeaders = HTTP_HEADERS;
string sessionCookie = between(responseHeaders, "Set-Cookie: ", ";");

if (sessionCookie == "") then
    throw "Login did not return a session cookie.";
end if

string headers =
    "Accept: application/json\r" +
    "Cookie: " + sessionCookie;

string privateData = HTTP_GET("https://api.example.com/private/data", headers);

If the server returns several Set-Cookie headers, collect the name=value part from each one and send them together in one Cookie: header separated by semicolon and space:

string headers =
    "Accept: application/json\r" +
    "Cookie: sessionid=abc123; theme=dark";

1.8. Headers

Headers are supplied as a single string. Separate multiple headers with carriage return (\r):

string headers =
    "Content-Type: application/json\r" +
    "Accept: application/json\r" +
    "X-Client: FileMaker";

Common headers:

Header Typical use
Accept: application/json Ask for JSON response.
Content-Type: application/json Send JSON request body.
Content-Type: text/xml Send XML/SOAP request body.
Authorization: Bearer <token> Bearer-token authentication.
Cookie: name=value Send cookies manually.

1.9. JSON API Example

function LookupCompany(string orgNr)
    orgNr = regex_replace("[^0-9]", orgNr, "");

    string url = "https://data.brreg.no/enhetsregisteret/api/enheter/" + orgNr;
    string headers = "Accept: application/vnd.brreg.enhetsregisteret.enhet.v2+json;charset=UTF-8";
    string response = HTTP_GET(url, headers);

    if (HTTP_STATUS_CODE != 200) then
        throw format("Company lookup failed. Status=%d Response=%s", HTTP_STATUS_CODE, response);
    end if

    JSON data = response;
    return JSON(
        "name", data["navn"],
        "zip", data["forretningsadresse.postnummer"],
        "city", data["forretningsadresse.poststed"]
    );
end

1.10. XML API Example

function SendXmlMessage(string endpoint, XML message)
    string headers = "Content-Type: text/xml\rAccept: text/xml";
    string response = HTTP_POST(endpoint, string(message), headers);

    if (HTTP_STATUS_CODE < 200 || HTTP_STATUS_CODE >= 300) then
        throw format("XML API call failed. Status=%d Response=%s", HTTP_STATUS_CODE, response);
    end if

    return response;
end

1.11. Notes