{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":1093,"path":"api-reference/server-api-reference/c_RESTResponseV2API.md","title":"RESTResponseV2 - Scoped, Global","bundle":null,"doc_type":null,"release":"australia","body":"\n# RESTResponseV2 - Scoped, Global\n\nThe RESTResponseV2 API provides methods that allow you to use the data returned by an outbound REST message in JavaScript code.\n\nA RESTResponseV2 object is returned by the RESTMessageV2 methods execute\\(\\) and executeAsync\\(\\).\n\nYou can use this API in scoped applications or within the global scope.\n\nThis API runs in the `sn_ws` namespace.\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## RESTResponseV2 - getAllHeaders\\(\\)\n\nReturns all headers contained in the response, including any duplicate headers.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|List&lt;GlideHTTPHeader&gt;|List of headers contained in the response. Each header is represented as a GlideHTTPHeader object which contains the header *name* and *value*.|\n\nThe follow code example shows how to call the REST Table API and then lists out all of the headers found in the response.\n\n```\nvar request = new sn_ws.RESTMessageV2();\nrequest.setEndpoint('http://<SN_Instance_host>/api/now/table/incident');\nrequest.setHttpMethod('GET');\n\nrequest.setBasicAuth('username', 'password');\nrequest.setRequestHeader(\"Accept\", \"application/json\");\nvar res = request.execute();\nif (res.getStatusCode() != 200) {\n  gs.info('Bad Request');\n}\n\nvar headers = res.getAllHeaders();\nfor(var i in headers){\n  gs.info(headers[i].name + ': ' + headers[i].value);\n}\n```\n\nResponse headers\n\n```\nX-Is-Logged-In: true\nX-Transaction-ID: e3003cb2db8c\nX-Total-Count: 66\nX-Content-Type-Options: nosniff\nPragma: no-store,no-cache\nCache-Control: no-cache,no-store,must-revalidate,max-age=-1\nExpires: 0\nContent-Type: application/json;charset=UTF-8\nDate: Fri, 14 May 2021 16:05:10 GMT\nConnection: close\nServer: ServiceNow\nJSESSIONID=5041870726ADADFAB2EE2BF8E7E01596; Path=/; HttpOnly; SameSite=None; Secure\nSet-Cookie: glide_user=; Max-Age=0; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Path=/; HttpOnly; SameSite=None; Secure\nSet-Cookie: glide_user_session=; Max-Age=0; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Path=/; HttpOnly; SameSite=None; Secure\nSet-Cookie: glide_user_route=glide.f1aa2015b3fa5fa8dfe567386cde8fd5; Max-Age=2147483647; Expires=Wed, 01-Jun-2089 19:19:17 GMT; Path=/; HttpOnly; SameSite=None; Secure\nSet-Cookie: glide_session_store=AF003CB2DB8CF8103DD9C39D139619C7; Max-Age=1800; Expires=Fri, 14-May-2021 16:35:10 GMT; Path=/; HttpOnly; SameSite=None; Secure\nSet-Cookie: BIGipServerpool_ddershem=2643023626.45630.0000; path=/; Httponly; Secure; SameSite=None; Secure\nStrict-Transport-Security: max-age=63072000; includeSubDomains\n```\n\n## RESTResponseV2 - getBody\\(\\)\n\nGets the content of the REST response body.\n\nUse this function when you want to get the request body as text content. Do not use this method when saving the response as a binary attachment. If a RESTMessageV2 object called the saveResponseBodyAsAttachment\\(...\\) function, using getBody\\(\\) on the associated RESTResponseV2 object will cause an error. When saving the response as an attachment, if the outbound REST message fails, call getErrorMessage\\(\\) on the response to retrieve the body content.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|String|REST response body.|\n\n```\nvar sm = new sn_ws.RESTMessageV2(\"Yahoo Finance\",\"get\"); //Might throw exception if message doesn't exist or not visible due to scope.\nvar response = sm.execute();\nvar responseBody = response.getBody();\n```\n\n## RESTResponseV2 - getCookies\\(\\)\n\nReturns all cookies included in the response.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|Object|Array of strings representing cookies. Iterate through the array to perform operations on each cookie.|\n\nDisplay individual cookies from the response.\n\n```\nvar cookies = response.getCookies();\nfor (var i = 0; i < cookies.length; i++) {\n  gs.info('cookie: ' + cookies.get(i));\n}\n```\n\nOutput:\n\n```\ncookie: JSESSIONID=4135AA97A5D12DA22EF614AA2B0CAFD8.node20; Path=/; Secure; HttpOnly\ncookie: SABASESSIONID=370152970.36895.0000; path=/\n```\n\n## RESTResponseV2 - getErrorCode\\(\\)\n\nGets the numeric error code if there was an error during the REST transaction.\n\nThis error code is specific to the ServiceNow AI Platform, it is not an HTTP error code. Provide this error code if you require assistance from ServiceNow Customer Support.\n\n**Note:** To retrieve the contents of the actual error message, use the [getBody\\(\\)](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/c_RESTResponseV2API.md) method.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|Number|Numeric error code, such as 1 for socket timeout.|\n\n```\nvar sm = new sn_ws.RESTMessageV2(\"Yahoo Finance\",\"get\"); //Might throw exception if message doesn't exist or not visible due to scope.\nvar response = sm.execute();\nvar errorCode = response.getErrorCode();\n```\n\n## RESTResponseV2 - getErrorMessage\\(\\)\n\nGets the error message if there was an error during the REST transaction.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|String|Error message.|\n\n```\nvar sm = new sn_ws.RESTMessageV2(\"Yahoo Finance\",\"get\"); //Might throw exception if message doesn't exist or not visible due to scope.\nvar response = sm.execute();\nvar errorMsg = response.getErrorMessage();\n```\n\n## RESTResponseV2 - getHeader\\(String name\\)\n\nGets the value for a specified header.\n\n|Name|Type|Description|\n|----|----|-----------|\n|name|String|Name of the header that you want the value for, such as Set-Cookie.|\n\n|Type|Description|\n|----|-----------|\n|String|Value of the specified header.|\n\n```\nvar sm = new sn_ws.RESTMessageV2(\"Yahoo Finance\",\"get\"); //Might throw exception if message doesn't exist or not visible due to scope.\nvar response = sm.execute();\nvar headerVal = response.getHeader(\"Content-Type\");\n```\n\n## RESTResponseV2 - getHeaders\\(\\)\n\nGets all headers returned in the REST response and the associated values.\n\n**Note:** If a header is present more than once in the response, such as a Set-Cookie header, this function returns only the last of the duplicate headers. To return all headers including duplicates, use the getAllHeaders\\(\\) function.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|Object|Object that maps the name of each header to the associated value.|\n\n```\nvar sm = new sn_ws.RESTMessageV2(\"Yahoo Finance\",\"get\"); //Might throw exception if message doesn't exist or not visible due to scope.\nvar response = sm.execute();\nvar headers = response.getHeaders();\n```\n\n## RESTResponseV2 - getQueryString\\(\\)\n\nGets the fully-resolved query sent to the REST endpoint..\n\nThis query contains the endpoint URL as well as any values assigned to variables in the REST message. Use this method only with responses to direct requests. This method is not supported for requests sent asynchronously, or requests sent using a MID server.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|String|Fully-resolved query.|\n\n```\nvar sm = new sn_ws.RESTMessageV2(\"Yahoo Finance\",\"get\"); //Might throw exception if message doesn't exist or not visible due to scope.\nvar response = sm.execute();\nvar queryString = response.getQueryString();\n```\n\n## RESTResponseV2 - getResponseAttachmentSysid\\(\\)\n\nGets the sys\\_id value of the attachment created from the response body content.\n\nIf the RESTMessageV2 object associated with this response called the saveResponseBodyAsAttachment\\(...\\) function, use getResponseAttachmentSysid\\(\\) to get the sys\\_id of the created attachment record. Use this function when you want to perform additional operations with the new attachment record.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|String|Sys\\_id of the new attachment record.|\n\n## RESTResponseV2 - getStatusCode\\(\\)\n\nGets the numeric HTTP status code returned by the REST provider.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|Number|Numeric status code returned by the REST provider, such as 200 for a successful response.|\n\n```\nvar sm = new sn_ws.RESTMessageV2(\"Yahoo Finance\",\"get\"); //Might throw exception if message doesn't exist or not visible due to scope.\nvar response = sm.execute();\nvar statusCode = response.getStatusCode();\n```\n\n## RESTResponseV2 - haveError\\(\\)\n\nIndicates if there was an error during the REST transaction.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|boolean|Returns true if there was an error, false if there was no error.|\n\n```\nvar sm = new sn_ws.RESTMessageV2(\"Yahoo Finance\",\"get\"); //Might throw exception if message doesn't exist or not visible due to scope.\nvar response = sm.execute();\nvar error = response.haveError();\n```\n\n## RESTResponseV2 - waitForResponse\\(Number timeoutSecs\\)\n\nSet the amount of time the instance waits for a response from the web service provider.\n\nThis method overrides the property glide.rest.outbound.ecc\\_response.timeout for this REST response.\n\n|Name|Type|Description|\n|----|----|-----------|\n|timeoutSecs|Number|The amount of time, in seconds, to wait for this response.|\n\n|Type|Description|\n|----|-----------|\n|void| |\n\n```\nvar sm = new sn_ws.RESTMessageV2(\"Yahoo Finance\",\"get\"); //Might throw exception if message doesn't exist or not visible due to scope.\nvar response = sm.executeAsync();\nresponse.waitForResponse(60);\n```\n\n"}}