http: sending and receiving http requests

The http library allows scripts to fetch and push data from and to the internet. It is a high-level interface with the Socket object. The library implements a good subset of the HTTP 1.1 protocol.

#include "extendables/extendables.jsx";
var http = require("http");
var response = http.get("http://www.w3c.org")
if (response.status_code == 200) {
    $.writeln(response.body);
} else {
    $.writeln("Connection failed");
}

Aside from high-level functions like get and post, there’s also a lower-level interface providing, if you happen to need more flexibility.

var req = new http.HTTPRequest("GET", "http://nytimes.com");
req.follow_redirects(false);
var timeout = req.timeout();
req.timeout(10);
$.writeln("Changing timeout from {} to {} seconds".format(timeout, 10));
req.header("User-Agent", "My ExtendScript app");
var res = req.do();
$.writeln(res.status == 200);

The Socket object is available in Adobe Bridge, Adobe InDesign, Adobe InCopy, Adobe After Effects and Adobe Photoshop, and you may also use it in the ExtendScript Toolkit. No luck for Illustrator fiends.

Basic requests

del

del(url[, basic_auth][, timeout])
Arguments:
  • url (String) – The URL for the resource
  • basic_auth (Object) – Basic authentication — any object with username and password

properties will do.

param Number timeout:
 How long before the http client should give up. (Default: 1)

Performs a DELETE request on the specified resource.

get

get(url[, basic_auth][, timeout])
Arguments:
  • url (String) – The URL for the resource
  • basic_auth (Object) – Basic authentication — any object with username and password properties will do.
  • timeout (Number) – How long before the http client should give up. (Default: 5)

Performs a GET request on the specified resource.

has_internet_access

has_internet_access()

Tests whether the application has access to the internet. If not, this might either imply that the user is simply not connected, or otherwise that a firewall is blocking internet access for the active Creative Suite app.

post

post(url, data[, basic_auth][, timeout])
Arguments:
  • url (String) – The URL for the resource
  • data (Object|String) – Either an object, to be urlencoded by this function, or a string that

will be passed along unchanged.

param Object basic_auth:
 Basic authentication — any object with username and password

properties will do.

param Number timeout:
 How long before the http client should give up. (Default: 5)

Performs a POST request on the specified resource.

put

put(url, data[, basic_auth][, timeout])
Arguments:
  • url (String) – The URL for the resource
  • data (Object|String) – Either an object, to be urlencoded by this function, or a string that

will be passed along unchanged.

param Object basic_auth:
 Basic authentication — any object with username and password

properties will do.

param Number timeout:
 How long before the http client should give up. (Default: 1)

Performs a PUT request on the specified resource. PUT requests are like POST requests, but idempotent.

Request objects

An incomplete but “good enough” implementation of the hypertext transfer protocol for the client side. This is a lower-level interface. It feeds the get(), head(), post(), put() and del() convenience functions.

Supports:

  • most HTTP methods: GET, HEAD, POST, PUT, DELETE
  • persistent connections
  • chunked responses
  • redirects

Soon:

  • focus on reliability, more tests
  • basic authentication

Most likely never:

  • digest authentication
  • cookies
  • proxies
  • caching / 304 Not Modified

HTTPRequest objects are entirely getter/setter-based, so e.g. use req.method() to get the current request method, and use req.method("POST") to change the request method.

var http = require("http");
 var example = "http://www.example.com"
 var response = http.HTTPRequest("GET", example);
 if (response.status == 200) {
     $.writeln(response.body);
 } else {
     $.writeln("Couldn't fetch {}".format(example));
 }
class HTTPRequest([method], url[, timeout])
Arguments:
  • method (String) –
  • url (String) –
  • timeout (Number) –

content

HTTPRequest#content(data)
Arguments:
  • data

Any content to send along with a PUT or POST request.

do

HTTPRequest#do()
Returns Object:Returns a HTTPResponse() object.

Executes the request.

encoding

HTTPRequest#encoding(encoding)
Arguments:
  • encoding

The character encoding in which to send this request, which is also the preferred response encoding.

follow_redirects

HTTPRequest#follow_redirects(value)
Arguments:
  • value

Whether to follow redirects when requesting a resource. By default, true for GET and HEAD requests, false for POST and PUT requests.

headers

HTTPRequest#headers(hash)
Arguments:
  • hash (Object) – A key-value object. Replaces all existing headers.

Use the header method instead when fetching or changing a single header.

The headers for this request. By default, these headers are included:

User-Agent
InDesign ExtendScript
Accept
/
Connection
close

max_redirects

HTTPRequest#max_redirects(value)
Arguments:
  • value

How much redirects the http client should follow before giving up.

method

HTTPRequest#method(type)
Arguments:
  • type

The request method. One of GET, HEAD, POST, PUT or DELETE. GET by default.

persistent

HTTPRequest#persistent(value)
Arguments:
  • value

Whether to establish a persistent connection. False by default, and best left that way.

port

HTTPRequest#port(number)
Arguments:
  • number

The server port the request should be directed to.

timeout

HTTPRequest#timeout(duration)
Arguments:
  • duration

How long before the http client should give up the request. 5 seconds by default.

url

HTTPRequest#url(url)
Arguments:
  • url

The resource to request

Attributes

auth

auth

Basic authentication Currently not implemented.

Response objects

The response to an HTTP request. These are returned by HTTPRequest() objects, you should never have to construct them yourself.

class HTTPResponse(method, encoding, request)
Arguments:
  • method – The request method.
  • encoding – The expected response encoding.
  • request – The original request; mainly handy for debugging.

Available as for_request on the response object.

body

body

The body content of the response.

corrupt

corrupt

Whether the response is corrupt. Currently not implemented.

encoding

encoding

The encoding of the response we received. Usually UTF-8.

for_request

for_request

The original HTTPRequest object that led to this response.

is_redirect

is_redirect

Whether we’ve received a response with a redirect code.

redirection_type

redirection_type

If redirected, distinguishes between requests that should be re-issued with a GET request (303) and those that should be re-issued as-is (all the others).

redirects

redirects

An array of any redirects the request might have followed.

response_time

response_time

How long it took to request the resource and get a response. In milliseconds.

status

status

The status code of the response.