HTTP Signature
The HTTP Signature for Scale Functions
The HTTP Signature for Scale Functions is meant to be used to implement HTTP handlers and middleware in supported guest languages.
It’s designed to allow developers to read and modify the incoming request as well as the outgoing response.
Usage
The HTTP Signature is implemented differently between the Guest and Host languages. The Host language has direct access to the HTTP Request and Response objects, while the Guest language has accessor methods to read and modify the request and response.
This allows the Host language complete control over the HTTP request and response, while the Guest language has stricter restrictions on what can be modified and how it can be modified.
Request
The HTTP Request object contains the following fields:
Field | Type | Description |
---|---|---|
uri | string | The URI of the request. |
method | enum | The HTTP method of the request. |
content_length | uint64 | The length of the request body. |
protocol | enum | The HTTP protocol of the request. |
ip | string | The IP address of the request. |
body | bytes | The request body. |
headers | string_map | The request headers. |
It can be accessed using the Request(): Request
method. The following are methods that can be used on the returned object.
URI
The uri
field is a string
that contains the URI of the request. It can be accessed using the URI(): string
method and
modified using the SetURI(uri: string)
method.
Method
The method
field is an enum
that contains the HTTP method of the request. It can be accessed using the Method(): method
method and modified using the SetMethod(method: method)
method.
Valid values for the method
field are:
GET
POST
PUT
PATCH
DELETE
HEAD
OPTIONS
Content Length
The content_length
field is a uint64
that contains the length of the request body. It can be accessed using the
ContentLength(): uint64
method and modified using the SetContentLength(length: uint64)
method.
Protocol
The protocol
field is an enum
that contains the HTTP protocol of the request. It can be accessed using the
Protocol(): protocol
method and modified using the SetProtocol(protocol: protocol)
method.
Valid values for the protocol
field are:
HTTP/1.1
HTTP/2
HTTP/3
IP
The ip
field is a string
that contains the IP address of the request. It can be accessed using the IP(): string
method
and modified using the SetIP(ip: string)
method.
Body
The body
field is a bytes
that contains the request body. It can be accessed using the Body(): []bytes
method and modified
using the SetBody(body: []bytes)
method.
If you’d like to set the body to a string, you can use the SetBodyString(body: string)
method.
Headers
The headers
field is a string_map
that contains the request headers. It can be accessed using the Header()
method.
Writing a key to the headers returned by the Header(): header
method can be done using the Set(key: string, value: []string)
method. Reading a key from
the headers returned by the Header(): header
method can be done using the Get(key: string): []string
method.
Response
The HTTP Response object contains the following fields:
Field | Type | Description |
---|---|---|
status_code | uint32 | The status code of the response. |
body | bytes | The response body. |
headers | string_map | The response headers. |
It can be accessed using the Response(): Response
method. The following are methods that can be used on the returned object.
Status Code
The status_code
field is a uint32
that contains the status code of the response. It can be accessed using the
StatusCode(): uint32
method and modified using the SetStatusCode(status_code: uint32)
method.
Body
The body
field is a bytes
that contains the response body. It can be accessed using the Body(): []bytes
method and modified
using the SetBody(body: []bytes)
method.
If you’d like to set the body to a string, you can use the SetBodyString(body: string)
method.
Headers
The headers
field is a string_map
that contains the response headers. It can be accessed using the Header()
method.
Writing a key to the headers returned by the Header(): header
method can be done using the Set(key: string, value: []string)
method. Reading a key from
the headers returned by the Header(): header
method can be done using the Get(key: string): []string
method.
Definition
The HTTP Signature is defined as follows using the Scale Signature syntax: