Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Expand
titlePass a table of parameters when calling your API

Using tables to store parameters allows you to dynamically build the data you need to send with the request. It also keeps your functions simple and support multiple optional parameters.

In this example, we are passing the endpoint, body and live parameter in a single table for apiCall() to use to perform the HTTP request.

We are also passing the method, to apiCall() which is set up to make various http calls (e.g., get, put, post.

Expand
titlePreparing Prepare the body or query string parameters according to your data format

In this example, we are working with JSON data. Notice in createOrder.lua we are first serializing the JSON as a string to prepare it for the body of the request.

Depending on the API you’re working with and the type of request, there are a few functions that you may want to use to prepare the body or query string parameters:

  • For JSON: json.serialize{} to convert a Lua table to a string

  • For XML: tostring() or :S() to convert an XML node tree to a string

  • For query strings: with net.http.get() use the parameters argument to pass a table of query parameters and values to automatically append them to the URL.

Expand
titleBuild in Add resiliency to you for HTTP requests using pcall and retry logic

Make your integration more robust by handling errors and retrying calls when necessary. This is particularly useful for handling intermittent network or server issues.

Now, looking at apiCall(), you can see pcall() and the Retry Library are used when making the net.http.* API call:

See Handling HTTP ResponseResponses for the fundamentals and recommendations on how to handle responses in IguanaX.

...