Class HTTP

Used to retrieve the result of a URL.

Examples

HTTP h;

Byte[] b = h.open("https://httpbin.org/");
printLine(h.getValue("statusCode"));

if (h.hasError())
  print(h.getErrorMessage());
else

Constructors

HTTP()

Initializes a new instance of the HTTP class.

Declaration

HTTP

Examples

HTTP h;

Byte[] b = h.open("https://httpbin.org/");
printLine(h.getValue("statusCode"));

if (h.hasError())
  print(h.getErrorMessage());
else

Methods

addAttachmentData(Integer)

Adds the binary data in the Service attachment specified to the body of the request.

Declaration

Bool addAttachmentData(Integer attachmentId)

Examples

#setLanguageLevel 3;

HTTP http;

if(http.addAttachmentData(24))
{
  Byte[] res = http.post("https://httpbin.org/post");
  if (!http.hasError())
    print(String(res));
  else
    print(http.getErrorMessage());

Parameters

Type Name Description
Integer attachmentId The ID of the Service attachment you want to upload.

Returns

Type Description
Bool True if the attachment was found; otherwise, false.

addBinaryData(Byte[])

Adds the binary data to the body of the request.

Declaration

Void addBinaryData(Byte[] binaryData)

Examples

#setLanguageLevel 3;

HTTP http;
Byte[] image = http.get("https://httpbin.org/image/png");
if (!http.hasError())
{
  http.addBinaryData(image);
  http.addHeader("Content-Type", "image/png");
  Byte[] res = http.post("https://httpbin.org/post");
  print(String(res));
}
else
{
  print(http.getErrorMessage());
}

Parameters

Type Name Description
Byte[] binaryData An array of Bytes containing the binary data you want to apply to the request body.

Returns

Type Description
Void

addDocumentData(Integer)

Add the binary data in the document specified to the body of the request.

Declaration

Bool addDocumentData(Integer documentId)

Examples

#setLanguageLevel 3;

HTTP http;

if(http.addDocumentData(24))
{
  Byte[] res = http.post("https://httpbin.org/post");
  if (!http.hasError())
    print(String(res));
  else
    print(http.getErrorMessage());
}

Parameters

Type Name Description
Integer documentId The ID of the document you want to upload.

Returns

Type Description
Bool True if the document was found; otherwise, false.

addHeader(String,String)

Adds a new header. The new header will be placed after other existing headers.

Declaration

Void addHeader(String name, String value)

Examples

HTTP h;

h.addHeader("header", "test");

Byte[] b = h.post("https://httpbin.org/post");

if (h.hasError())
print(h.getErrorMessage());
else
print(String(b));

Parameters

Type Name Description
String name Name of header
String value Value of header.

Returns

Type Description
Void

delete(String)

Supports both HTTP and HTTPS.

Declaration

Byte[] delete(String url)

Examples

HTTP h;

Byte[] b = h.delete("https://httpbin.org/delete");
if (h.hasError())
  print(h.getErrorMessage());
else

Parameters

Type Name Description
String url What to delete.

Returns

Type Description
Byte[] The result in a byte array.

deleteAsStream(String)

Supports both HTTP and HTTPS.

Declaration

NSStream deleteAsStream(String url)

Examples

HTTP h;

NSStream b = h.deleteAsStream("https://httpbin.org/delete");
if (h.hasError())
  print(h.getErrorMessage());
else

Parameters

Type Name Description
String url What to delete.

Returns

Type Description
NSStream

get(String)

Supports both HTTP and HTTPS.

Declaration

Byte[] get(String url)

Examples

HTTP h;

h.addHeader("header", "test");
h.setValue("key", "value");
h.setValue("key2", "value2");
h.setOption("followLocation", "true");
Byte[] b = h.get("https://httpbin.org/get");
if (h.hasError())
  print(h.getErrorMessage());
else

Parameters

Type Name Description
String url

Returns

Type Description
Byte[] The result in a byte array.

getAsStream(String)

Supports both HTTP and HTTPS.

Declaration

NSStream getAsStream(String url)

Examples

HTTP h;

h.addHeader("header", "test");
h.setValue("key", "value");
h.setValue("key2", "value2");
h.setOption("followLocation", "true");
NSStream b = h.getAsStream("https://httpbin.org/get");
if (h.hasError())
  print(h.getErrorMessage());
else

Parameters

Type Name Description
String url

Returns

Type Description
NSStream The result.

getDebug()

Retrieves debug output.

Declaration

String getDebug()

Returns

Type Description
String The debug output.

getErrorMessage()

Returns the last error message.

Declaration

String getErrorMessage()

Examples

HTTP h;

h.addHeader("header", "test");
h.setValue("key", "value");
h.setValue("key2", "value2");
h.setOption("followLocation", "true");
Byte[] b = h.put("https://httpbin.org/put");

if (h.hasError())
  print(h.getErrorMessage());
else

Returns

Type Description
String The last error message.

getResponseHeader(String)

Gets a named HTTP header from the response of the HTTP request.

Declaration

String getResponseHeader(String header)

Parameters

Type Name Description
String header The header to get.

Returns

Type Description
String The header.

getResponseHeaders()

Gets a map of all the headers from the HTTP response headers after making a HTTP call.

Declaration

Map getResponseHeaders()

Returns

Type Description
A map of all the headers from the HTTP response headers after making a HTTP call.

getValue(String)

General function which returns various values from the instance.

Declaration

String getValue(String name)

Parameters

Type Name Description
String name What to get (statusCode).

Returns

Type Description
String Values from the instance.

hasError()

Checks if there was an error in the environment.

Declaration

Bool hasError()

Returns

Type Description
Bool True if there was an error in the environment; otherwise, false.

open(String)

Opens a URL and returns the result as a Byte array.

Declaration

Byte[] open(String url)

Examples

HTTP h;

Byte[] b = h.open("https://httpbin.org/");

if (h.hasError())
  print(h.getErrorMessage());
else

Parameters

Type Name Description
String url The URL to open. http://...

Returns

Type Description
Byte[] Result as a Byte array.

openAsStream(String)

Opens a URL and returns the result as an NSStream.

Declaration

NSStream openAsStream(String url)

Examples

HTTP h;

NSStream b = h.openAsStream("https://httpbin.org/");

if (h.hasError())
  print(h.getErrorMessage());
else

Parameters

Type Name Description
String url The URL to open. http://...

Returns

Type Description
NSStream

patch(String)

Supports both HTTP and HTTPS.

Declaration

Byte[] patch(String url)

Examples

HTTP h;

h.addHeader("header", "test");
h.setValue("key", "value");
h.setValue("key2", "value2");
h.setOption("followLocation", "true");
Byte[] b = h.patch("https://httpbin.org/patch");
if (h.hasError())
  print(h.getErrorMessage());
else

Parameters

Type Name Description
String url

Returns

Type Description
Byte[] The result as a byte array.

patchAsStream(String)

Supports both HTTP and HTTPS.

Declaration

NSStream patchAsStream(String url)

Examples

HTTP h;

h.addHeader("header", "test");
h.setValue("key", "value");
h.setValue("key2", "value2");
h.setOption("followLocation", "true");
NSStream b = h.patchAsStream("https://httpbin.org/patch");
if (h.hasError())
  print(h.getErrorMessage());
else

Parameters

Type Name Description
String url

Returns

Type Description
NSStream The result.

post(String)

Supports both HTTP and HTTPS.

Declaration

Byte[] post(String url)

Examples

HTTP h;

h.addHeader("header", "test");
h.setValue("key", "value");
h.setValue("key2", "value2");
h.setOption("followLocation", "true");
Byte[] b = h.post("https://httpbin.org/post");
if (h.hasError())
  print(h.getErrorMessage());
else

Parameters

Type Name Description
String url

Returns

Type Description
Byte[] The result as a byte array.

postAsStream(String)

Supports both HTTP and HTTPS.

Declaration

NSStream postAsStream(String url)

Examples

HTTP h;

h.addHeader("header", "test");
h.setValue("key", "value");
h.setValue("key2", "value2");
h.setOption("followLocation", "true");
NSStream b = h.postAsStream("https://httpbin.org/post");
if (h.hasError())
  print(h.getErrorMessage());
else

Parameters

Type Name Description
String url

Returns

Type Description
NSStream The result.

putAsStream(String)

Supports both HTTP and HTTPS.

Declaration

NSStream putAsStream(String url)

Examples

HTTP h;

h.addHeader("header", "test");
h.setValue("key", "value");
h.setValue("key2", "value2");
h.setOption("followLocation", "true");
NSStream b = h.putAsStream("https://httpbin.org/put");
if (h.hasError())
  print(h.getErrorMessage());
else

Parameters

Type Name Description
String url

Returns

Type Description
NSStream The result.

setDebugMode()

Turns debug mode on/off to record debug output.

Declaration

Void setDebugMode(Bool debug)

Parameters

Type Name Description
Bool debug True to turn debug on, false to turn it off.

Returns

Type Description
Void

setOption(String,Bool)

Option function.

Declaration

Void setOption(String name, String value)

Examples

HTTP h;

h.addHeader("header", "test");
h.setValue("key", "value");
h.setValue("key2", "value2");
h.setOption("followLocation", "true");

Byte[] b = h.post("https://httpbin.org/post");

if (h.hasError())
  print(h.getErrorMessage());
else
  print(String(b));

//Here is an example for creating an issue in JIRA, hosted by Atlassian

HTTP h2;
String json = " {
  "fields": {
    "project": {
      "key": "SUP"
    },
"summary": "I have a problem!.",
"description": "Thats awesome",
    "issuetype": {
      "name": "Bug"
    }
  }
}";

HTTP h;
h.setOption("username", "theUsername");
h.setOption("password", "thePassword");
h.addHeader("Content-Type", "application/json");
h.setOption("parameters", json);
print(String(h.post("https://theInstance.atlassian.net/rest/api/2/issue/")));

Parameters

Type Name Description
String name
String value See table.

Returns

Type Description
Void

setValue(String,String)

Adds a CGI variable and its according value to the HTTP request.

Declaration

Void setValue(String cgiVariable, String value)

Examples

HTTP h;

h.addHeader("header", "test");
h.setValue("key", "value");
h.setValue("key2", "value2");
h.setOption("followLocation", "true");
Byte[] b = h.put("https://httpbin.org/put");
if (h.hasError())
  print(h.getErrorMessage());
else

Parameters

Type Name Description
String cgiVariable The variable to set
String value The value.

Returns

Type Description
Void