-
Notifications
You must be signed in to change notification settings - Fork 3
Working with Requests
cooffeeRequired edited this page May 9, 2025
·
6 revisions
This guide covers how to work with HTTP requests using skJson.
# Create a GET request
set {_request} to prepare GET request on "https://api.example.com/data"
# Create a POST request
set {_request} to prepare POST request on "https://api.example.com/create"
# Create other HTTP methods
set {_request} to prepare PUT request on "https://api.example.com/update"
set {_request} to prepare DELETE request on "https://api.example.com/delete"
set {_request} to prepare PATCH request on "https://api.example.com/patch"
# Set headers
set {_request}'s headers to "{'Content-Type': 'application/json'}"
# Set query parameters
set {_request}'s query params to "key:value", "page:1"
# Set request body
set {_request}'s body to json from "{'name': 'John'}"
# Set attachments
set {_request}'s attachments to attachment("*/test.json")
# Execute request and wait for response
execute {_request}
# Get response
set {_response} to {_request}'s response
# Execute request without blocking
execute {_request} as non blocking
# Handle response in event
on http response received:
set {_response} to event-response
# Get response body
set {_body} to body of {_response}
# Get response headers
set {_headers} to headers of {_response}
# Get status code
set {_status} to status code of {_response}
# Check if request was successful
if status of {_response} is "OK":
send "Request successful!"
else:
send "Request failed!"
# Access specific header
set {_content_type} to value "Content-Type" of headers of {_response}
# Loop through headers
loop values of headers of {_response}:
send "Header: %json-key% = %json-value%"
- Always handle errors appropriately
- Use appropriate HTTP methods
- Set proper headers
- Handle timeouts
- Validate responses
- Use meaningful variable names
- Implement proper error handling
# Create API request
set {_request} to prepare GET request on "https://api.example.com/users"
set {_request}'s headers to "{'Authorization': 'Bearer %{_token}%'}"
# Execute request
execute {_request}
# Handle response
set {_response} to {_request}'s response
if status of {_response} is "OK":
set {_users} to body of {_response}
send "Users loaded successfully!"
# Create upload request
set {_request} to prepare POST request on "https://api.example.com/upload"
set {_request}'s headers to "{'Content-Type': 'multipart/form-data'}"
set {_request}'s attachments to attachment("*/test.json")
# Execute request
execute {_request} as non blocking
# Handle response
on received response:
if status of event-response is "OK":
send "File uploaded successfully!"
# Create request with query parameters
set {_request} to prepare GET request on "https://api.example.com/search"
set {_request}'s query params to "q:test", "page:1", "limit:10"
# Execute and handle response
execute {_request}
set {_response} to {_request}'s response
if status of {_response} is "OK":
send body of {_response}
# Set timeout in headers
set {_request}'s headers to "{'Timeout': '5000'}"
# Handle timeout
if status of {_response} is "FAILED":
send "Request timed out!"
# Check response status code
if status code of {_response} is 404:
send "Resource not found!"
else if status code of {_response} is 500:
send "Server error!"
# Validate request before sending
if {_request}'s body is set:
execute {_request}
else:
send "Request body is required!"
- See how to Modify JSON
- Return to Working with Cache
- Check out Working with Files
- View Examples for more usage examples
- Learn about Creating JSON
Note
For more information about Markdown formatting and alerts, see GitHub's discussion on Markdown alerts.