Skip to content

Working with Requests

cooffeeRequired edited this page May 9, 2025 · 6 revisions

Working with Requests 🌐

This guide covers how to work with HTTP requests using skJson.

Request Basics πŸ“

Creating Requests

# 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"

Setting Request Properties

# 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")

Executing Requests πŸš€

Synchronous Requests

# Execute request and wait for response
execute {_request}

# Get response
set {_response} to {_request}'s response

Asynchronous Requests

# Execute request without blocking
execute {_request} as non blocking

# Handle response in event
on http response received:
    set {_response} to event-response

Working with Responses πŸ“₯

Accessing Response Data

# 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}

Checking Response Status

# Check if request was successful
if status of {_response} is "OK":
    send "Request successful!"
else:
    send "Request failed!"

Response Headers

# 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%"

Best Practices πŸ’‘

  1. Always handle errors appropriately
  2. Use appropriate HTTP methods
  3. Set proper headers
  4. Handle timeouts
  5. Validate responses
  6. Use meaningful variable names
  7. Implement proper error handling

Examples πŸ“š

API Integration

# 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!"

File Upload

# 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!"

Query Parameters

# 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}

Common Issues and Solutions πŸ”

Timeout Handling

# Set timeout in headers
set {_request}'s headers to "{'Timeout': '5000'}"

# Handle timeout
if status of {_response} is "FAILED":
    send "Request timed out!"

Error Handling

# 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!"

Request Validation

# Validate request before sending
if {_request}'s body is set:
    execute {_request}
else:
    send "Request body is required!"

Next Steps πŸ‘£

Note

For more information about Markdown formatting and alerts, see GitHub's discussion on Markdown alerts.