{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":1317,"path":"api-reference/web-services/r_RESTMessageV2MIDServerExample.md","title":"RESTMessageV2 MID server example","bundle":null,"doc_type":null,"release":"australia","body":"\n# RESTMessageV2 MID server example\n\nYou can send an outbound REST message through a MID Server.\n\nBy sending the message through a MID Server, you can access endpoints that are behind a firewall or within a private network. All REST messages sent through a MID Server are asynchronous.\n\n```\nvar requestBody;\nvar responseBody;\nvar status;\nvar sm;\ntry{\n\tsm = new sn_ws.RESTMessageV2(\"Yahoo Finance\", \"get\");  // Might throw exception if message doesn't exist or not visible due to scope.\n\tsm.setBasicAuth(\"admin\",\"admin\");\n\tsm.setStringParameter(\"symbol\", \"NOW\");\n\tsm.setStringParameterNoEscape(\"xml_data\",\"<data>test</data>\");\n\tsm.setMIDServer('mid_server_name');\n\tresponse = sm.executeAsync(); // Might throw exception if http connection timed out or some issue with sending request itself because of encryption/decryption of password.\n\n\tresponse.waitForResponse(60); // In seconds. Wait at most 60 seconds to get response from ECC Queue/Mid Server //Might throw exception timing out waiting for response in ECC queue.\n\n\tresponseBody = response.haveError() ? response.getErrorMessage() : response.getBody();\n\tstatus = response.getStatusCode();\n} catch(ex) {\n\tresponseBody = ex.getMessage();\n\tstatus = '500';\n} finally {\n\trequestBody = sm ? sm.getRequestBody():null;\n}\ngs.info(\"Request Body: \" + requestBody);\ngs.info(\"Response: \" + responseBody);\ngs.info(\"HTTP Status: \" + status);\n```\n\n**Note:** This example uses `waitForResponse` to pause for a response, and then details how the response is handled. However, when using `executeAsync`, consider processing the response body in a separate business rule to take advantage of the asynchronous call rather than using `waitForResponse`.\n\n**Parent Topic:**[Scripting outbound REST](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/web-services/c_ScriptingOutboundREST.md)\n\n"}}