Can ios send payloads?
Author’s note: I’m wondering how to use the shortcuts app to curl an endpoint
Capabilities Summary
Yes, iOS can send HTTP payloads to web APIs through the built-in Shortcuts app. This functionality is primarily handled by the ‘Get Contents of URL’ action, which effectively allows a user to replicate curl-style requests without needing third-party applications. The Shortcuts app supports making HTTP requests using methods such as GET, POST, PUT, PATCH, and DELETE. It allows for the inclusion of custom HTTP headers, which is essential for tasks like authentication (e.g., sending ‘Authorization’ headers with Bearer or Basic tokens). Furthermore, it can send request bodies formatted as JSON, form-encoded data, or as multipart/form-data for file uploads. After a request is made, Shortcuts has native capabilities to parse JSON responses directly into dictionaries and lists, enabling the data to be used in subsequent actions within a workflow.
Get Contents Of Url Action Overview
The ‘Get Contents of URL’ action is the foundation for all HTTP networking within the iOS Shortcuts app. To use it, a user typically first adds a ‘URL’ action to define the API endpoint and then passes this URL as input to the ‘Get Contents of URL’ action. By default, it performs a GET request. However, by tapping ‘Show More’, a user can access advanced parameters to customize the request. These parameters include:
- Method: Allows selection of the HTTP method, with options for GET, POST, PUT, PATCH, and DELETE.
- Headers: A section to add custom HTTP headers, such as
Content-TypeorAuthorization, which are necessary for interacting with most APIs. - Request Body: This parameter appears specifically when the method is set to POST, PUT, or PATCH. It allows the user to define the payload to be sent to the server. The body can be configured as JSON (by adding key-value pairs), a Form (for
application/x-www-form-urlencodedormultipart/form-datarequests), or a File. The action’s output is the response from the server, which can then be passed to other actions for parsing, such as ‘Get Dictionary from Input’ for JSON responses.
Sending Json Payloads
To send a JSON payload using the iOS Shortcuts app, utilize the ‘Get Contents of URL’ action. First, add a ‘URL’ action to define the API endpoint. Follow this with the ‘Get Contents of URL’ action and expand its options by tapping ‘Show More’. Set the ‘Method’ to POST, PUT, or PATCH, which will reveal a ‘Request Body’ parameter. Choose ‘JSON’ as the request body type. This provides an interface to add key-value pairs to construct your JSON object. For example, to post to https://jsonplaceholder.typicode.com/posts, you would set the method to POST and add keys such as title, body, and userId with their corresponding string or number values. The Content-Type header is typically set to application/json automatically, but it can be added manually in the ‘Headers’ section for certainty. After the request executes, the JSON response can be processed by subsequent actions like ‘Get Dictionary from Input’ to extract data for further use in the shortcut.
Sending Form Data And Files
The ‘Get Contents of URL’ action in Shortcuts facilitates sending data as both application/x-www-form-urlencoded and multipart/form-data through the ‘Form’ request body type. To send URL-encoded data, which is equivalent to curl -d, set the ‘Request Body’ to ‘Form’ and add key-value pairs as text fields. For instance, to submit a login form, you would add fields for ‘username’ and ‘password’. To upload a file using multipart/form-data, which mirrors curl -F, you also set the ‘Request Body’ to ‘Form’. In this case, add a new field and, instead of text, select a variable containing a file (e.g., from a ‘Select Photos’ or ‘Select File’ action). This action automatically formats the request as multipart. You can include other text fields alongside the file field, such as a ‘description’. While a dedicated ‘File’ option exists for the request body, community reports indicate it may not work reliably, making the ‘Form’ method the recommended and most stable approach for all file uploads.
Response Handling And Json Parsing
After making an API request using the ‘Get Contents of URL’ action in the Shortcuts app, the returned data, often in JSON format, can be processed and utilized in subsequent steps. The primary method for this is to use the ‘Get Dictionary from Input’ action. This action takes the JSON text output from the API call and converts it into a structured Shortcuts dictionary. Once the data is in a dictionary format, you can extract specific pieces of information using the ‘Get Dictionary Value’ action. For more complex JSON structures, such as lists (arrays) of objects, you can use ‘Repeat’ loops to iterate through each item in the list and extract values from the objects within. Apple’s official guides on using JSON in Shortcuts explain that JSON structures data into dictionaries (key-value pairs) and lists, and these guides provide detailed workflows for parsing this data, such as accessing values from nested objects and iterating over lists to handle multiple results.
Redirect Cookie And Header Access Limitations
The ‘Get Contents of URL’ action in iOS Shortcuts has significant limitations regarding the handling of HTTP responses. According to community and developer reports, the action does not provide a way to access the response headers from a network request. This means it is impossible to inspect critical information such as status codes (beyond a simple success/failure), ‘Set-Cookie’ headers for session management, or rate limit information. Furthermore, the action’s behavior with HTTP redirects is reported to be unreliable and not configurable. It may not follow redirects in all cases, and there is no mechanism to capture the final URL after a redirect has occurred. This lack of access to headers and control over redirects prevents the implementation of complex, stateful API interactions that rely on cookies or analyzing redirect chains.
Timeout And Rate Limit Considerations
When using the ‘Get Contents of URL’ action, there are two primary external constraints to consider: request timeouts and API rate limits. First, while not officially documented, user reports suggest that network requests have an unofficial timeout limit, potentially around 60 seconds. This means that any request to an endpoint that takes longer than this to respond (e.g., for long-running tasks like AI processing or large data generation) will fail. Shortcuts should be designed to interact with faster endpoints or to stage work in smaller chunks. Second, Apple’s documentation explicitly warns that API rate limits and usage caps from the service provider apply. When building shortcuts that make multiple requests, especially within loops, it is crucial to respect these limits to avoid being blocked or throttled by the API server. This requires careful workflow design, such as batching requests or introducing delays, rather than using tight, rapid-fire loops.