Crayon

AboutTutorialsDocumentationDemosDownloadSource

Http

Library for sending HTTP requests.

enum Method[link]

Various common HTTP Methods. Any function that accepts this enum is also capable of accepting a string for custom or obscure HTTP methods.

NameDescription
GET

POST

PUT

DELETE

enum RequestMode[link]

Enum representing what format the response should be parsed as.

NameDescription
TEXT

Response should be treated as a text string.

BINARY

Response should be treated as a list of byres.

enum StatusCode[link]

Enum for standard status codes. Note that a status code received by a server can be arbitrary and may not necessarily be represented in this enum. Each enum value's integer value is identical to the code it represents e.g. the enum value StatusCode.NOT_FOUND resolves to the integer 404.

NameDescription
CONTINUE

100

SWITCHING_PROTOCOLS

101

PROCESSING

102

CHECKPOINT

103

OK

200

CREATED

201

ACCEPTED

202

NON_AUTHORITATIVE_INFORMATION

203

NO_CONTENT

204

RESET_CONTENT

205

PARTIAL_CONTENT

206

MULTI_STATUS

207

ALREADY_REPORTED

208

IM_USED

226

MULTIPLE_CHOICES

300

MOVED_PERMANENTLY

301

MOVED_TEMPORARILY

302

SEE_OTHER

303

NOT_MODIFIED

304

USE_PROXY

305

SWITCH_PROXY

306

TEMPORARY_REDIRECT

307

PERMANENT_REDIRECT

308

BAD_REQUEST

400

UNAUTHORIZED

401

PAYMENT_REQUIRED

402

FORBIDDEN

403

NOT_FOUND

404

METHOD_NOT_ALLOWED

405

NOT_ACCEPTABLE

406

PROXY_AUTHENTICATION_REQUIRED

407

REQUEST_TIMEOUT

408

CONFLICT

409

GONE

410

LENGTH_REQUIRED

411

PRECONDITION_FAILED

412

PAYLOAD_TOO_LARGE

413

URI_TOO_LONG

414

UNSUPPORTED_MEDIA_TYPE

415

RANGE_NOT_SATISFIABLE

416

EXPECTATION_FAILED

417

IM_A_TEAPOT

418

ENHANCE_YOUR_CALM

420

MISDIRECTED_REQUEST

421

UNPROCESSABLE_ENTITY

422

LOCKED

423

FAILED_DEPENDENCY

424

UPGRADE_REQUIRED

426

PRECONDITION_REQUIRED

428

TOO_MANY_REQUESTS

429

REQUEST_HEADER_FIELDS_TOO_LARGE

431

LOGIN_TIMEOUT

440

RETRY_WITH

449

BLOCKED_BY_PARENTAL_CONTROLS

450

UNAVAILABLE_FOR_LEGAL_REASONS

451

INTERNAL_SERVER_ERROR

500

NOT_IMPLEMENTED

501

BAD_GATEWAY

502

SERVICE_UNAVAILABLE

503

GATEWAY_TIMEOUT

504

HTTP_VERSION_NOT_SUPPORTED

505

VARIANT_ALSO_NEGOTIATES

506

INSUFFICIENT_STORAGE

507

LOOP_DETECTED

508

BANDWIDTH_LIMIT_EXCEEDED

509

NOT_EXTENDED

510

NETWORK_AUTHENTICATION_REQIURED

511

class HttpRequest[link]

Builds, sends, and manages an outgoing HTTP request.

new HttpRequest(url)[link]

Instantiates a new HTTP request, but does not send the request until you invoke .send() or .sendAsync().

Arguments

NameTypeDescription
url string

URL to send the request to.

function getResponse()[link]

Gets the HTTP response. Code must check the value of .isDone() before accessing this value. Throws an Core.InvalidOperationException if the value of .isDone() is false. Furthermore, an exception will also be thrown if .isDone() was never called on the request, even if the request is complete.

Return Value

TypeDescription
Http.HttpResponse

Response of the HTTP request.

function isDone()[link]

Checks to see if the request has completed if sent with .sendAsync().

Return Value

TypeDescription
boolean

True if the request has completed (in either an error or success).

function send()[link]

Sends the HTTP request and waits for a response. This function will block execution of your program. To make a request without blocking, see .sendAsync().

Return Value

TypeDescription
Http.HttpResponse

Response of the request.

function sendAsync()[link]

Sends the HTTP request and returns immediately while the request runs in the background. To check the status of the request, use .isDone() and .getResponse(). If blocking execution of code is okay, use .send().

function setContent(value, contentType)[link]

Sets the content of the outgoing request as a UTF-8 encoded text string. Optionally, you can set the Content-Type at the same time, which is redundant with calling .setHeader('Content-Type', value), but more convenient. If you want to send binary content in the request body, see setContentBytes().

Arguments

NameTypeOptionalDescription
value string

The content of the outgoing request.

contentType string Optional

The Content-Type header of the outgoing request

Return Value

TypeDescription
Http.HttpRequest

Returns a reference to itself to allow for builder syntax.

function setContentBytes(value, contentType)[link]

Sets the content of the outgoing request as a list of bytes. All Values will be modded by 256 before being sent. Optionally, you can set the Content-Type at the same time, which is redundant with calling .setHeader('Content-Type', value), but more convenient. If you want to send text content in the request body, see setContentString().

Arguments

NameTypeOptionalDescription
value List of integers

The content of the outgoing request.

contentType string Optional

The Content-Type header of the outgoing request

Return Value

TypeDescription
Http.HttpRequest

Returns a reference to itself to allow for builder syntax.

function setHeader(name, value)[link]

Sets a header with the given name. If invoked multiple times with the same header name, multiple headers will be sent (rather than) overwriting the previous value.

Arguments

NameTypeDescription
name string

Name of the header to set.

value string

Value of the header to set.

Return Value

TypeDescription
Http.HttpRequest

Returns a reference to itself to allow for builder syntax.

function setMethod(method)[link]

Sets the HTTP method for the outgoing request. If this is one of the common 4 values (GET, POST, PUT, or DELETE), the Http.Method enum can be used. If it is a custom or obscure method, an arbitrary string can be passed in instead.

Arguments

NameTypeDescription
method Http.Method or string

Method to use for the outgoing HTTP request.

Return Value

TypeDescription
Http.HttpRequest

Returns a reference to itself to allow for builder syntax.

function setMode(mode)[link]

Indicates to the request whether the body of the response should be treated as text or raw bytes.

Arguments

NameTypeDescription
mode Http.RequestMode

Expected format of the response.

Return Value

TypeDescription
Http.HttpRequest

Returns a reference to itself to allow for builder syntax.

class HttpResponse[link]

Contains the contents of an HTTP Response from a server.

function getContent()[link]

Returns the content of the body of the response. If the mode of the originating request was TEXT, then the return value will be a string. If the mode of the originating request was BINARY, then the return value will be a list of integers representing bytes. If there is no body, an empty string or empty list will be returned.

Return Value

TypeDescription
string or List of integers

The content of the body of the response.

function getHeader()[link]

Gets the string value of the given header name. Lookup is case-insensitive. If no header is found, null is returned. If there are multiple headers, only one is returned. To get all headers with that name, use .getHeaders(name).

Return Value

TypeDescription
string

The first header with that name or null.

function getHeaderNames()[link]

Returns the list of the names of the headers in an HTTP response. This can be used in conjunction with .getHeader(name) or .getHeaders(name).

Return Value

TypeDescription
List of strings

A list of the header names in the HTTP response

function getHeaders()[link]

Gets all the string values for the header with the given name. Lookup is case-insensitive and if multiple headers exist with different casing, all are still returned. If no header is found, the list will be empty. To get a default header as a string (as opposed to a list) use .getHeader(name).

Return Value

TypeDescription
List of strings

The headers with that name.

function getStatusCode()[link]

Returns the status code of the response. Value will be 0 if the request has not completed. The return value will always be an integer, but will commonly be a value in the Http.StatusCode enum.

Return Value

TypeDescription
Http.StatusCode or integer

The status code of the response.

function getStatusMessage()[link]

Returns the status message of the response. This is the brief text description that is generally associated with the status code. For example, the "NOT FOUND" in "404 NOT FOUND". This value can be arbitrary as it is provided by the server and may not necessarily adhere to well-known values.

Return Value

TypeDescription
string

The status message of the response.