Skip to content

HTTPRequest ​

com.jsmacrosce.jsmacros.core.library.impl.classes.HTTPRequest

Since1.1.8

A request configured with headers and timeouts, then sent synchronously to obtain a Response. Use the send overloads for methods or bodies not covered by the convenience methods; repeated sends open new connections using the current fields.
Responses for status codes 400 and above are returned normally with the error stream as their body; connection or I/O failures throw IOException.

js
// Configure once, then send.  
const request = Request.create("https://example.com/api/echo")
  .addHeader("Content-Type", "application/json")
  .addHeader("User-Agent", "JsMacrosCE");  

const response = request.post('{"ping": true}');  
print(`status ${response.responseCode}`);  
print(response.text());  

// HTTP error statuses are responses too.  
if (response.responseCode === 400) {
  print(`the server refused it: ${response.text()}`);  
}

// The same request can be sent again with another method.  
const second = request.send("PUT", "replacement");  
print(`that one came back with ${second.responseCode} and ${second.text().length} characters`);

Nested types ​

Constructors ​

new HTTPRequest ​

new HTTPRequest(String url)#
Parses and stores a URL for later requests; this does not open a connection or send a request. An invalid URL string causes MalformedURLException.

Parameters:

  • url: the URL string to parse

Fields ​

conn ​

conn: URL#
URL opened for each send. The field is public and may be reassigned between sends.

connectTimeout ​

connectTimeout: int#
Connection timeout in milliseconds, applied to each newly opened connection. Zero means no timeout.

headers ​

headers: Map<String, String>#
Headers to add to each request, keyed by name. This map is created per instance; addHeader(String, String) updates it, replacing an existing entry with the same key.

readTimeout ​

readTimeout: int#
Read timeout in milliseconds, applied to each newly opened connection. Zero means no timeout.

Methods ​

addHeader ​

addHeader(String key, String value): HTTPRequestSince1.1.8#
Adds or replaces a header entry in headers.

Parameters:

  • key: the header name, sent as it is given
  • value: the header value
Returns: self for chaining

get ​

get(): HTTPRequest.ResponseSince1.1.8#
sends the request as a GET and hands back what came of it. The headers are put on first and the method is set to GET, and the body of the request is whatever the platform sends for a request without one. The call blocks until the server answers or the connection fails. A status of four hundred or more is not a failure here: the error stream is read instead of the input stream, so the body is the server's own error page and Response.responseCode is there to be checked. A connection that never gets that far is an IOException.
const response = Request.create("https://example.com/api/status")
  .addHeader("Accept", "application/json")
  .get();
if (response.responseCode === 200) {
  print(response.text());
} else {
  print(`the server said ${response.responseCode}`);
}
Returns: the response, with its body still unread; for an error status, the body stream may be null if the connection supplies no error body

post ​

post(String data): HTTPRequest.ResponseSince1.1.8#
sends the request as a POST with a text body and hands back what came of it. The text is encoded as UTF-8 and its length is put on as the Content-Length, so the caller does not have to set either. The headers are on the request first, which means a Content-Length added through addHeader is replaced by the one computed here rather than being sent twice. The call blocks, and a status of four hundred or more comes back as a response carrying the server's error page rather than as a failure.
const response = Request.create("https://example.com/api/echo")
  .addHeader("Content-Type", "text/plain")
  .post("hello");
print(`status ${response.responseCode}, body ${response.text()}`);

Parameters:

  • data: the body, which is encoded as UTF-8
Returns: the response, with its body still unread
post(byte[] data): HTTPRequest.ResponseSince1.8.4#
Sends a POST request with the given bytes as its body, without encoding or modifying them. The byte count is sent as Content-Length; status codes 400 and above are returned as responses using the error stream as the body.

Parameters:

  • data: the body, which is sent as it is
Returns: the response, with its body still unread

put ​

put(String data): HTTPRequest.ResponseSince1.8.4#
Sends a PUT request with a text body encoded as UTF-8. The byte count is sent as Content-Length; use put to send bytes without text encoding.

Parameters:

  • data: the body, which is encoded as UTF-8
Returns: the response, with its body still unread
put(byte[] data): HTTPRequest.ResponseSince1.8.4#
Sends a PUT request with the given bytes as its body, without encoding or modifying them. The byte count is sent as Content-Length.

Parameters:

  • data: the body, which is sent as it is
Returns: the response, with its body still unread

send ​

send(String method): HTTPRequest.ResponseSince1.8.6#
Sends a request with the given method and no body. The method is passed to the connection unchanged; a method unsupported by the platform is rejected there. Use a body-taking overload to send a body.
const response = Request.create("https://example.com/api/session/1")
  .addHeader("Authorization", "Bearer not-a-real-token")
  .send("DELETE");
print(response.responseCode);

Parameters:

  • method: the HTTP method to use, which must be one the platform knows
Returns: the response, with its body still unread
send(String method, String data): HTTPRequest.ResponseSince1.8.4#
Sends a request with the given method and a text body encoded as UTF-8. The byte count is sent as Content-Length; the method is passed to the connection unchanged.

Parameters:

  • method: the HTTP method to use, which must be one the platform knows
  • data: the body, which is encoded as UTF-8
Returns: the response, with its body still unread
send(String method, byte[] data): HTTPRequest.ResponseSince1.8.4#
Sends a request with the given method and the given bytes as its body, without encoding or modifying them. The byte count is sent as Content-Length; the method is passed to the connection unchanged.

Parameters:

  • method: the HTTP method to use, which must be one the platform knows
  • data: the body, which is sent as it is
Returns: the response, with its body still unread

setConnectTimeout ​

setConnectTimeout(int timeout): HTTPRequestSince1.8.6#
Sets the connection-establishment timeout in milliseconds for subsequent sends. Zero means no timeout. This does not change a connection that has already been opened or impose an overall deadline on the complete request.

Parameters:

  • timeout: the connection timeout, in milliseconds
Returns: self for chaining

setReadTimeout ​

setReadTimeout(int timeout): HTTPRequestSince1.8.6#
Sets the response-read timeout in milliseconds for subsequent sends. Zero means no timeout. This limits waiting for response data rather than the total duration of a request, and does not change connections that have already been opened.

Parameters:

  • timeout: the read timeout, in milliseconds
Returns: self for chaining