> ## Documentation Index
> Fetch the complete documentation index at: https://docs.superoffice.com/llms.txt
> Use this file to discover all available pages before exploring further.

# HTTP

> Used to retrieve the result of a URL.

Used to retrieve the result of a URL.

## Constructors

### HTTP()

```crmscript theme={null}
HTTP
```

Initializes a new instance of the HTTP class.

## Methods

## addAttachmentData(Integer)

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

```crmscript theme={null}
Bool addAttachmentData(Integer attachmentId)
```

**Returns:** [CRMScript.Global.Bool](CRMScript.Global.Bool) - True if the attachment was found; otherwise, false.

<Note>
  Must be used together with POST, PUT or PATCH.

  Many REST endpoints expect the content to be uploaded as binary data when adding files. You can use this method for doing that. automatically set the Content-Type header based on the content type recorded in the database for the attachment. If you want to override the Content-Type, be sure to set a Content-Type header **after** doing the addAttachmentData call.
</Note>

**Example:**

```crmscript theme={null}
#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());
```

## addBinaryData(Byte\[])

Adds the binary data to the body of the request.

```crmscript theme={null}
Void addBinaryData(Byte[] binaryData)
```

**Returns:** [CRMScript.Global.Void](CRMScript.Global.Void)

<Note>
  Must be used together with POST, PUT or PATCH.

  Many REST endpoints expect the content to be uploaded as binary data when adding files. You can use this method for doing that. Normally, you also want to add a Content-Type header, indicating what kind of files this is.
</Note>

**Example:**

```crmscript theme={null}
#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());
}
```

## addDocumentData(Integer)

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

```crmscript theme={null}
Bool addDocumentData(Integer documentId)
```

**Returns:** [CRMScript.Global.Bool](CRMScript.Global.Bool) - True if the document was found; otherwise, false.

<Note>
  Must be used together with POST, PUT or PATCH.

  Many REST endpoints expect the content to be uploaded as binary data when adding files. You can use this method for doing that. automatically set the Content-Type header based on the extension of the document being added. If you want to override the Content-Type, be sure to set a Content-Type header **after** doing the addDocumentData call.
</Note>

**Example:**

```crmscript theme={null}
#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());
}
```

## addHeader(String,String)

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

```crmscript theme={null}
Void addHeader(String name, String value)
```

**Returns:** [CRMScript.Global.Void](CRMScript.Global.Void)

**Example:**

```crmscript theme={null}
HTTP h;
h.addHeader("header", "test");
Byte[] b = h.post("https://httpbin.org/post");
if (h.hasError())
print(h.getErrorMessage());
else
print(String(b));
```

## delete(String)

Supports both HTTP and HTTPS.

```crmscript theme={null}
Byte[] delete(String url)
```

**Returns:** [CRMScript.Global.Byte](CRMScript.Global.Byte)\[] - The result in a byte array.

**Example:**

```crmscript theme={null}
HTTP h;
Byte[] b = h.delete("https://httpbin.org/delete");
if (h.hasError())
  print(h.getErrorMessage());
else
```

## deleteAsStream(String)

Supports both HTTP and HTTPS.

```crmscript theme={null}
NSStream deleteAsStream(String url)
```

**Returns:** [CRMScript.NetServer.NSStream](CRMScript.NetServer.NSStream)

**Example:**

```crmscript theme={null}
HTTP h;
NSStream b = h.deleteAsStream("https://httpbin.org/delete");
if (h.hasError())
  print(h.getErrorMessage());
else
```

## get(String)

Supports both HTTP and HTTPS.

```crmscript theme={null}
Byte[] get(String url)
```

**Returns:** [CRMScript.Global.Byte](CRMScript.Global.Byte)\[] - The result in a byte array.

**Example:**

```crmscript theme={null}
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
```

## getAsStream(String)

Supports both HTTP and HTTPS.

```crmscript theme={null}
NSStream getAsStream(String url)
```

**Returns:** [CRMScript.NetServer.NSStream](CRMScript.NetServer.NSStream) - The result.

**Example:**

```crmscript theme={null}
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
```

## getDebug()

Retrieves debug output.

```crmscript theme={null}
String getDebug()
```

**Returns:** [CRMScript.Global.String](CRMScript.Global.String) - The debug output.

<Note>
  Turn on debug mode by calling setDebugMode(true) before you use getDebug().
</Note>

## getErrorMessage()

Returns the last error message.

```crmscript theme={null}
String getErrorMessage()
```

**Returns:** [CRMScript.Global.String](CRMScript.Global.String) - The last error message.

**Example:**

```crmscript theme={null}
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
```

## getResponseHeader(String)

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

```crmscript theme={null}
String getResponseHeader(String header)
```

**Returns:** [CRMScript.Global.String](CRMScript.Global.String) - The header.

<Note>
  Case insensitive.
</Note>

## getResponseHeaders()

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

```crmscript theme={null}
Map getResponseHeaders()
```

**Returns:** [CRMScript.Native.Map](CRMScript.Native.Map) - A map of all the headers from the HTTP response headers after making a HTTP call.

<Note>
  The key is in lower case regardless of what was returned by the response. The value will keep the case.
</Note>

## getValue(String)

General function which returns various values from the instance.

```crmscript theme={null}
String getValue(String name)
```

**Returns:** [CRMScript.Global.String](CRMScript.Global.String) - Values from the instance.

<Note>
  statusCode: Will return the status code from the HTTP request (such as 404, 200). Supported from v8.0sr3.
</Note>

## hasError()

Checks if there was an error in the environment.

```crmscript theme={null}
Bool hasError()
```

**Returns:** [CRMScript.Global.Bool](CRMScript.Global.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.

```crmscript theme={null}
Byte[] open(String url)
```

**Returns:** [CRMScript.Global.Byte](CRMScript.Global.Byte)\[] - Result as a Byte array.

**Example:**

```crmscript theme={null}
HTTP h;
Byte[] b = h.open("https://httpbin.org/");
if (h.hasError())
  print(h.getErrorMessage());
else
```

## openAsStream(String)

Opens a URL and returns the result as an NSStream.

```crmscript theme={null}
NSStream openAsStream(String url)
```

**Returns:** [CRMScript.NetServer.NSStream](CRMScript.NetServer.NSStream)

**Example:**

```crmscript theme={null}
HTTP h;
NSStream b = h.openAsStream("https://httpbin.org/");
if (h.hasError())
  print(h.getErrorMessage());
else
```

## patch(String)

Supports both HTTP and HTTPS.

```crmscript theme={null}
Byte[] patch(String url)
```

**Returns:** [CRMScript.Global.Byte](CRMScript.Global.Byte)\[] - The result as a byte array.

**Example:**

```crmscript theme={null}
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
```

## patchAsStream(String)

Supports both HTTP and HTTPS.

```crmscript theme={null}
NSStream patchAsStream(String url)
```

**Returns:** [CRMScript.NetServer.NSStream](CRMScript.NetServer.NSStream) - The result.

**Example:**

```crmscript theme={null}
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
```

## post(String)

Supports both HTTP and HTTPS.

```crmscript theme={null}
Byte[] post(String url)
```

**Returns:** [CRMScript.Global.Byte](CRMScript.Global.Byte)\[] - The result as a byte array.

**Example:**

```crmscript theme={null}
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
```

## postAsStream(String)

Supports both HTTP and HTTPS.

```crmscript theme={null}
NSStream postAsStream(String url)
```

**Returns:** [CRMScript.NetServer.NSStream](CRMScript.NetServer.NSStream) - The result.

**Example:**

```crmscript theme={null}
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
```

## putAsStream(String)

Supports both HTTP and HTTPS.

```crmscript theme={null}
NSStream putAsStream(String url)
```

**Returns:** [CRMScript.NetServer.NSStream](CRMScript.NetServer.NSStream) - The result.

**Example:**

```crmscript theme={null}
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
```

## setDebugMode()

Turns debug mode on/off to record debug output.

```crmscript theme={null}
Void setDebugMode(Bool debug)
```

**Returns:** [CRMScript.Global.Void](CRMScript.Global.Void)

## setOption(String,Bool)

Option function.

```crmscript theme={null}
Void setOption(String name, String value)
```

**Returns:** [CRMScript.Global.Void](CRMScript.Global.Void)

<Note>
  | Value                | Description                                                                                                                                                                                                                                                                                                                                                                        |
  | -------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
  | verifyPeer           | verify the peer's SSL certificate                                                                                                                                                                                                                                                                                                                                                  |
  | verifyHost           | verify the certificate's name against the host                                                                                                                                                                                                                                                                                                                                     |
  | timeout              | set maximum time the request is allowed to take                                                                                                                                                                                                                                                                                                                                    |
  | followLocation       | follow HTTP 3xx redirects if set to 1, default is 1                                                                                                                                                                                                                                                                                                                                |
  | username             | username used in authentication                                                                                                                                                                                                                                                                                                                                                    |
  | password             | password used in authentication                                                                                                                                                                                                                                                                                                                                                    |
  | parameters           | The complete parameters to post. This will replace whatever you have added using .setValue(), but allows you to specify the whole parameter to post. Useful when posting e.g. JSON structs instead of name=value pairs. NOTE you need to prefix whatever you want to send with a dummy character (which will be removed) due to complexities in this class. See the example below. |
  | authenticationMethod | which authentication method to use basic (default), digest, gssnegotiate, ntlm, digest\_ie, ntlm\_wb, none                                                                                                                                                                                                                                                                         |
  | parametersUTF8       | From version 8.2R3. This option makes the parameters be encoded as UTF-8. Normally this is what you want, but to not break any old uses, this is an optional option.                                                                                                                                                                                                               |
</Note>

**Example:**

```crmscript theme={null}
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/")));
```

## setValue(String,String)

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

```crmscript theme={null}
Void setValue(String cgiVariable, String value)
```

**Returns:** [CRMScript.Global.Void](CRMScript.Global.Void)

**Example:**

```crmscript theme={null}
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
```


## Related topics

- [kb_http_link table](/en/database/tables/kb-http-link.md)
- [Securing HTTPS configuration on Windows Server](/en/onsite/security/secure-https.md)
- [Http Post Save Ticket Entity With Notify](/en/api/reference/restful/rest/ticket/http-post-save-ticket-entity-with-notify.md)
- [Http Put Save Ticket Entity With Notify](/en/api/reference/restful/rest/ticket/http-put-save-ticket-entity-with-notify.md)
- [Http Post Save Ticket Message Entity With Notify](/en/api/reference/restful/rest/ticketmessage/http-post-save-ticket-message-entity-with-notify.md)
- [Http Put Save Ticket Message Entity With Notify](/en/api/reference/restful/rest/ticketmessage/http-put-save-ticket-message-entity-with-notify.md)
