{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":797,"path":"api-reference/server-api-reference/GlideHTTPRequestAPI.md","title":"GlideHTTPRequest - Global","bundle":null,"doc_type":null,"release":"australia","body":"\n# GlideHTTPRequest- Global\n\nThe GlideHTTPRequest API provides utility methods to perform common functions with Glide HTTP requests.\n\nYou can use this API in global server-side scripts. To use this class, instantiate a GlideHTTPRequest object using the constructor. The constructor requires an endpoint URL as an input parameter.\n\n**Parent Topic:**[Server API reference](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/api-server.md)\n\n## GlideHTTPRequest - addHeader\\(String name, String value\\)\n\nAdds a header to your HTTP request.\n\n|Name|Type|Description|\n|----|----|-----------|\n|name|String|Header name, such as `Accept` or `Content-Type`.|\n|value|String|Header value, such as `application/json`.|\n\n|Type|Description|\n|----|-----------|\n|void| |\n\nThis example adds the request header \"Accept\" and returns the number of the incidents from a ServiceNow Instance, parsing the JSON or XML responses.\n\n```\nvar instance = 'dev12345';\nvar username = 'admin';\nvar password = 'yourpassword';\n\n// Instantiate request with ServiceNow API incidents table endpoint\nvar request = new GlideHTTPRequest('https://'+instance+'.service-now.com/api/now/table/incident');\n\n// Add authentication data\nrequest.setBasicAuth(username, password);\n\n// Add the Accept header to get JSON response\nrequest.addHeader('Accept', 'application/json');\n\n// Execute the GET request\nvar response = request.get();\n\n// Print the results: status code and number of records returned\ngs.print(response.getStatusCode());\ngs.print('(JSON) Incidents returned: ' + JSON.parse(response.getBody()).result.length);\n\n// Replace the Accept header to get XML response\nrequest.addHeader('Accept', 'application/xml');\n\n// Execute the GET request\nvar response = request.get();\n\n// Print the results: status code and number of records returned\ngs.print(response.getStatusCode());\ngs.print('(XML) Incidents returned: ' + gs.xmlToJSON(response.getBody()).response.result.length);\n```\n\nOutput\n\n```\n200\n(JSON) Incidents returned: 66\n200\n(XML) Incidents returned: 66\n```\n\n## GlideHTTPRequest - addParameter\\(String name, String value\\)\n\nAdds a parameter to your HTTP request.\n\n|Name|Type|Description|\n|----|----|-----------|\n|name|String|Parameter to add, such as **sysparm\\_limit**.|\n|value|String|Value for the parameter.|\n\n|Type|Description|\n|----|-----------|\n|void| |\n\nThis example shows how to add the **sysparm\\_limit** parameter to the REST endpoint call to limit the number of returned responses.\n\n```\nvar instance = 'dev12345';\nvar username = 'admin';\nvar password = 'yourpassword';\n\n// Instantiate request with ServiceNow API incidents table endpoint\nvar request = new GlideHTTPRequest('https://'+instance+'.service-now.com/api/now/table/incident');\n\n// Add authentication data\nrequest.setBasicAuth(username, password);\n\n// Add the 'sysparm_limit' parameter to limit the number of records returned\nrequest.addParameter('sysparm_limit', 1);\n\n// Execute the GET request\nvar response = request.get();\n\n// Print the results: status code and number of records returned\ngs.print(response.getStatusCode());\ngs.print('Incidents returned: ' + JSON.parse(response.getBody()).result.length);\n```\n\nOutput:\n\n```\n200\nIncidents returned: 1\n```\n\n## GlideHTTPRequest - setBasicAuth\\(String userName, String password\\)\n\nSets a user name and password for basic authentication.\n\n|Name|Type|Description|\n|----|----|-----------|\n|userName|String|User name to use for authentication.|\n|password|String|User's password to use for authentication.|\n\n|Type|Description|\n|----|-----------|\n|void| |\n\nThis example shows how to use the setBasicAuth\\(\\) method to set the user name and password for the associated REST endpoint call.\n\n```\nvar instance = 'dev12345';\nvar username = 'admin';\nvar password = 'yourpassword';\n\n// Instantiate request with ServiceNow API incidents table endpoint\nvar request = new GlideHTTPRequest('https://'+instance+'.service-now.com/api/now/table/incident');\n\n// Add authentication data\nrequest.setBasicAuth(username, password);\n\n// Add the Accept header to get JSON response\nrequest.addHeader('Accept', 'application/json');\n\n// Execute the GET request\nvar response = request.get();\n\n// Print the results: status code and number of records returned\ngs.print(response.getStatusCode());\ngs.print('(JSON) Incidents returned: ' + JSON.parse(response.getBody()).result.length);\n\n// Replace the Accept header to get XML response\nrequest.addHeader('Accept', 'application/xml');\n\n// Execute the GET request\nvar response = request.get();\n\n// Print the results: status code and number of records returned\ngs.print(response.getStatusCode());\ngs.print('(XML) Incidents returned: ' + gs.xmlToJSON(response.getBody()).response.result.length);\n```\n\nOutput\n\n```\n200\n(JSON) Incidents returned: 66\n200\n(XML) Incidents returned: 66\n```\n\n## GlideHTTPRequest - setContentType\\(String type\\)\n\nSets the Content-Type header for your HTTP request to the specified value.\n\n|Name|Type|Description|\n|----|----|-----------|\n|type|String|Content type to set, such as `application/json` or `multipart/form-data`. For additional information on Content-Type, see [https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type).|\n\n|Type|Description|\n|----|-----------|\n|void| |\n\nThis example shows how to set the `Content-Type` request header for a REST endpoint call using the setContentType\\(\\) method.\n\n```\nvar instance = 'dev12345';\nvar username = 'admin';\nvar password = 'yourpassword';\n\n// Instantiate request with ServiceNow API incidents table endpoint\nvar request = new GlideHTTPRequest('https://'+instance+'.service-now.com/api/now/table/incident');\n\n// Add authentication data\nrequest.setBasicAuth(username, password);\n\n// Set up incident record to post\n\n// Set the Content-Type of the POST\nrequest.setContentType('application/json');\n\n// Execute the POST request\nvar response = request.post();\n\n// Print the results: status code and number of records returned\ngs.print(response.getStatusCode());\n```\n\nOutput\n\n```\n200\n\n```\n\n## GlideHTTPRequest - setFollowRedirect\\(boolean followRedirect\\)\n\nEnables or disables the follow redirect option for a REST endpoint call.\n\nFor additional information on HTTP redirects, see [https://developer.mozilla.org/en-US/docs/Web/HTTP/Redirections](https://developer.mozilla.org/en-US/docs/Web/HTTP/Redirections).\n\n<table id=\"table_kzt_pwy_cfb\" class=\"parameters\"><thead><tr><th>\n\nName\n\n</th><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nfollowRedirect\n\n</td><td>\n\nBoolean\n\n</td><td>\n\nFlag that indicates whether the endpoint should follow any URL redirect returned by the endpoint. Valid values:\n\n-   true: Following any returned redirect.\n-   false: Ignore any returned redirect.\n\nDefault: true\n\n</td></tr></tbody>\n</table>|Type|Description|\n|----|-----------|\n|void| |\n\nThis example shows how to turn off redirects for an endpoint call using the setFollowRedirect\\(\\) method.\n\n```\nvar instance = 'dev12345';\nvar username = 'admin';\nvar password = 'yourpassword';\n\n// Instantiate request with ServiceNow API incidents table endpoint\nvar request = new GlideHTTPRequest('https://'+instance+'.service-now.com/api/now/table/incident');\n\n// Add authentication data\nrequest.setBasicAuth(username, password);\n\n// Add the Accept header to get JSON response\nrequest.addHeader('Accept', 'application/json');\n\n// Turn off follow redirect - default is on (true)\nrequest.setFollowRedirect(false);\n\n// Execute the GET request\nvar response = request.get();\n\n// Print the results: status code and number of records returned\ngs.print(response.getStatusCode());\n```\n\nOutput\n\n```\n200\n```\n\n## GlideHTTPRequest - setHttpTimeout\\(int timeout\\)\n\nSets the HTTP timeout value in milliseconds.\n\n<table id=\"table_rjp_1ly_cfb\" class=\"parameters\"><thead><tr><th>\n\nName\n\n</th><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\ntimeout\n\n</td><td>\n\nInteger\n\n</td><td>\n\nTimeout value to set.Unit: Milliseconds\n\n</td></tr></tbody>\n</table>|Type|Description|\n|----|-----------|\n|void| |\n\nThis example shows how to set the time out value for an endpoint call using the setTimeout\\(\\) method.\n\n```\nvar instance = 'dev12345';\nvar username = 'admin';\nvar password = 'yourpassword';\n\n// Instantiate request with ServiceNow API incidents table endpoint\nvar request = new GlideHTTPRequest('https://'+instance+'.service-now.com/api/now/table/incident');\n\n// Add authentication data\nrequest.setBasicAuth(username, password);\n\n// Add the Accept header to get JSON response\nrequest.addHeader('Accept', 'application/json');\n\n// Set the time out value\nrequest.setHttpTimeOut(1000);\n\n// Execute the GET request\nvar response = request.get();\n\n// Print the results: status code and number of records returned\ngs.print(response.getStatusCode());\n```\n\nOutput\n\n```\n200\n```\n\n## GlideHTTPRequest - setLogLevel\\(String logLevel\\)\n\nSets the log level for an HTTP request.\n\n<table id=\"table_ijx_myy_cfb\" class=\"parameters\"><thead><tr><th>\n\nName\n\n</th><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nlogLevel\n\n</td><td>\n\nString\n\n</td><td>\n\nThe level of logging available. **Note:** For performance reasons, in production it is advisable to leave HTTP request logging at **basic**.\n\n Valid values:\n\n -   basic: Covers many of the attributes of the HTTP transaction including host, path, response status, and so on.\n-   elevated: Includes all of **basic**, as well as all request headers, the query string, and all response headers.\n-   all: Includes all of **elevated**, as well as the request body and the response body.\n\n Default: basic\n\n</td></tr></tbody>\n</table>|Type|Description|\n|----|-----------|\n|void| |\n\nThis example shows how to set the log level for an endpoint call using the setLogLevel\\(\\) method.\n\n```\nvar instance = 'dev12345';\nvar username = 'admin';\nvar password = 'yourpassword';\n\n// Instantiate request with ServiceNow API incidents table endpoint\nvar request = new GlideHTTPRequest('https://'+instance+'.service-now.com/api/now/table/incident');\n\n// Add authentication data\nrequest.setBasicAuth(username, password);\n\n// Add the Accept header to get JSON response\nrequest.addHeader('Accept', 'application/json');\n\n// Set the time out value\nrequest.setLogLevel(elevated);\n\n// Execute the GET request\nvar response = request.get();\n\n// Print the results: status code and number of records returned\ngs.print(response.getStatusCode());\n```\n\nOutput\n\n```\n200\n```\n\n## GlideHTTPRequest - setupProxy\\(String host, String port\\)\n\nSets the proxy host and port for the associated REST call.\n\n|Name|Type|Description|\n|----|----|-----------|\n|host|String|Proxy host|\n|port|String|Proxy port|\n\n|Type|Description|\n|----|-----------|\n|void| |\n\n"}}