Skip to main content

HTTP Protocol

HTTP (Hypertext Transfer Protocol) is the foundation of data communication on the web. AppBlocks provides comprehensive HTTP client and server capabilities for RESTful APIs and web services.

Overview​

HTTP in AppBlocks enables:

  • REST API calls to cloud services
  • Web service integration
  • Webhook reception
  • Custom API endpoints
  • Data synchronization

HTTP Methods​

GET​

Retrieve data from server:

GET /api/temperature
Response: {"value": 22.5, "unit": "C"}

Use cases:

  • Query sensor data
  • Fetch configuration
  • Health checks
  • Status queries

POST​

Submit data to server:

POST /api/data
Body: {"device_id": "001", "value": 25}
Response: {"status": "success"}

Use cases:

  • Send telemetry
  • Submit forms
  • Create resources
  • Upload data

PUT​

Update existing resource:

PUT /api/settings/temp
Body: {"setpoint": 22}
Response: {"updated": true}

Use cases:

  • Update configuration
  • Modify settings
  • Replace data

DELETE​

Remove resource:

DELETE /api/log/123
Response: {"deleted": true}

Use cases:

  • Clear logs
  • Remove entries
  • Cleanup operations

Request Structure​

Request Line​

POST /api/endpoint HTTP/1.1

Headers​

Host: api.example.com
Content-Type: application/json
Content-Length: 45
Authorization: Bearer token123
User-Agent: AppBlocks/1.0

Body​

{
"device_id": "sensor-001",
"temperature": 22.5,
"timestamp": "2023-10-09T12:00:00Z"
}

Response Structure​

Status Line​

HTTP/1.1 200 OK

Headers​

Content-Type: application/json
Content-Length: 87
Cache-Control: no-cache

Body​

{
"status": "success",
"data": {
"id": 123,
"processed": true
}
}

Status Codes​

Success (2xx)​

  • 200 OK: Request successful
  • 201 Created: Resource created
  • 204 No Content: Success, no data to return

Redirection (3xx)​

  • 301 Moved Permanently: Resource relocated
  • 302 Found: Temporary redirect
  • 304 Not Modified: Use cached version

Client Error (4xx)​

  • 400 Bad Request: Invalid request format
  • 401 Unauthorized: Authentication required
  • 403 Forbidden: Access denied
  • 404 Not Found: Resource doesn't exist
  • 429 Too Many Requests: Rate limit exceeded

Server Error (5xx)​

  • 500 Internal Server Error: Server problem
  • 502 Bad Gateway: Upstream server error
  • 503 Service Unavailable: Server overloaded
  • 504 Gateway Timeout: Upstream timeout

Headers​

Common Request Headers​

Content-Type Specify body format:

Content-Type: application/json
Content-Type: application/x-www-form-urlencoded
Content-Type: text/plain

Authorization Authentication credentials:

Authorization: Bearer <token>
Authorization: Basic <base64-credentials>
Authorization: ApiKey <key>

Accept Desired response format:

Accept: application/json
Accept: text/html
Accept: */*

User-Agent Client identification:

User-Agent: AppBlocks/1.0 (Device-001)

Common Response Headers​

Content-Type Response body format:

Content-Type: application/json; charset=utf-8

Cache-Control Caching directives:

Cache-Control: no-cache
Cache-Control: max-age=3600

Set-Cookie Set client cookies:

Set-Cookie: session=abc123; HttpOnly; Secure

Content Types​

JSON (Most Common)​

Content-Type: application/json

{
"temperature": 22.5,
"humidity": 65
}

Form Data​

Content-Type: application/x-www-form-urlencoded

device_id=001&value=25&unit=celsius

Plain Text​

Content-Type: text/plain

Temperature: 22.5 degrees

Binary Data​

Content-Type: application/octet-stream

[binary data]

Authentication​

API Key​

GET /api/data
Authorization: ApiKey your-api-key-here

Bearer Token​

POST /api/command
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...

Basic Auth​

GET /api/status
Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=

REST API Patterns​

Resource-Based URLs​

GET    /api/devices          # List all devices
GET /api/devices/123 # Get specific device
POST /api/devices # Create new device
PUT /api/devices/123 # Update device
DELETE /api/devices/123 # Delete device

Query Parameters​

GET /api/data?start=2023-01-01&end=2023-12-31&type=temperature

Pagination​

GET /api/logs?page=2&limit=50
Response: {
"data": [...],
"page": 2,
"total_pages": 10
}

Error Handling​

Error Response Format​

{
"error": {
"code": "INVALID_INPUT",
"message": "Temperature value out of range",
"details": {
"field": "temperature",
"value": 150,
"max": 100
}
}
}

Retry Logic​

1. Send request
2. If timeout or 5xx error:
- Wait (exponential backoff)
- Retry up to 3 times
3. If 4xx error:
- Don't retry
- Handle error

HTTPS/TLS​

Secure Communication​

https://api.example.com/endpoint

Benefits:

  • Encrypted data transmission
  • Server authentication
  • Data integrity
  • Prevents eavesdropping

Certificate Validation​

  • Verify server certificate
  • Check certificate expiration
  • Validate certificate chain
  • Pin certificates (advanced)

Performance​

Optimization Tips​

Keep-Alive Reuse TCP connections:

Connection: keep-alive

Compression Reduce bandwidth:

Accept-Encoding: gzip, deflate
Content-Encoding: gzip

Conditional Requests Use caching:

If-Modified-Since: Mon, 09 Oct 2023 12:00:00 GMT
If-None-Match: "abc123"

Batch Requests Combine multiple operations

AppBlocks Implementation​

HTTP Client​

Use HTTP Request block:

  • Make HTTP requests
  • Parse responses
  • Handle errors
  • Process data

HTTP Server​

Use HTTP Server feature:

  • Create endpoints
  • Handle requests
  • Generate responses
  • Implement APIs

Common Use Cases​

Send Telemetry​

POST https://api.example.com/telemetry
Content-Type: application/json

{
"device_id": "sensor-001",
"readings": [
{"type": "temperature", "value": 22.5},
{"type": "humidity", "value": 65}
],
"timestamp": "2023-10-09T12:00:00Z"
}

Receive Commands​

GET https://api.example.com/commands/sensor-001

Response:
{
"commands": [
{"id": 1, "action": "restart"},
{"id": 2, "action": "set_interval", "value": 60}
]
}

Weather API​

GET https://api.weather.com/v1/current?location=Boston

Response:
{
"temperature": 18.5,
"humidity": 72,
"conditions": "Partly Cloudy"
}

Best Practices​

  1. Use HTTPS for sensitive data
  2. Implement proper error handling
  3. Set reasonable timeouts
  4. Validate all inputs
  5. Handle rate limiting
  6. Log requests for debugging
  7. Use appropriate status codes
  8. Version your APIs
  9. Document endpoints
  10. Test with various scenarios

Troubleshooting​

Common HTTP issues:

  • Timeout: Increase timeout or check network
  • 404 Not Found: Verify URL spelling
  • 401 Unauthorized: Check credentials
  • Certificate Error: Verify HTTPS certificate
  • Connection Refused: Check server status

See Also​