"use strict";(self.webpackChunkdocs_openc3_com=self.webpackChunkdocs_openc3_com||[]).push([[7593],{2748:(e,n,i)=>{i.r(n),i.d(n,{assets:()=>l,contentTitle:()=>r,default:()=>d,frontMatter:()=>o,metadata:()=>c,toc:()=>a});var t=i(1085),s=i(1184);const o={title:"Testing with Curl"},r=void 0,c={id:"development/curl",title:"Testing with Curl",description:"This information is just generally used behind the scenes in COSMOS tools",source:"@site/docs/development/curl.md",sourceDirName:"development",slug:"/development/curl",permalink:"/tools/staticdocs/docs/development/curl",draft:!1,unlisted:!1,editUrl:"https://github.com/OpenC3/cosmos/tree/main/docs.openc3.com/docs/development/curl.md",tags:[],version:"current",frontMatter:{title:"Testing with Curl"},sidebar:"defaultSidebar",previous:{title:"Development",permalink:"/tools/staticdocs/docs/development"},next:{title:"Developing COSMOS",permalink:"/tools/staticdocs/docs/development/developing"}},l={},a=[{value:"Curl Example with OpenC3 COSMOS Open Source",id:"curl-example-with-openc3-cosmos-open-source",level:2},{value:"Curl Example with OpenC3 COSMOS Enterprise",id:"curl-example-with-openc3-cosmos-enterprise",level:2},{value:"Suite Runner Example",id:"suite-runner-example",level:2}];function p(e){const n={admonition:"admonition",code:"code",h2:"h2",img:"img",li:"li",ol:"ol",p:"p",pre:"pre",ul:"ul",...(0,s.R)(),...e.components};return(0,t.jsxs)(t.Fragment,{children:[(0,t.jsx)(n.admonition,{title:"This documentation is for COSMOS Developers",type:"note",children:(0,t.jsx)(n.p,{children:"This information is just generally used behind the scenes in COSMOS tools"})}),"\n",(0,t.jsx)(n.p,{children:"The COSMOS APIs are all served over HTTP, which means you can use curl to experiment with them. Curl is a great tool for seeing exactly how the API responds to any given request."}),"\n",(0,t.jsx)(n.h2,{id:"curl-example-with-openc3-cosmos-open-source",children:"Curl Example with OpenC3 COSMOS Open Source"}),"\n",(0,t.jsx)(n.p,{children:"OpenC3 COSMOS Open Source just has a single user account, so all you need to do is pass the single password as the token with your requests like this."}),"\n",(0,t.jsx)(n.p,{children:"Request:"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-bash",children:'curl -i -H "Content-Type: application/json-rpc" -H "Authorization: password" -d \'{"jsonrpc": "2.0", "method": "tlm", "params": ["INST HEALTH_STATUS TEMP1"], "keyword_params": {"scope": "DEFAULT"}, "id": 8}\' -X POST http://127.0.0.1:2900/openc3-api/api\n'})}),"\n",(0,t.jsx)(n.p,{children:"Response:"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-bash",children:'HTTP/1.1 200 OK\nCache-Control: max-age=0, private, must-revalidate\nContent-Length: 51\nContent-Type: application/json-rpc\nEtag: W/"e806aacfdbed0b325e7a5928e3bb5cf4"\nVary: Origin\nX-Request-Id: bbad6c6b-6d22-4374-a86f-b5b0b95e6939\nX-Runtime: 0.059044\nDate: Sat, 04 Nov 2023 21:34:47 GMT\n\n{"jsonrpc":"2.0","id":8,"result":53.26555000000001}\n'})}),"\n",(0,t.jsx)(n.h2,{id:"curl-example-with-openc3-cosmos-enterprise",children:"Curl Example with OpenC3 COSMOS Enterprise"}),"\n",(0,t.jsx)(n.p,{children:"OpenC3 COSMOS Enterprise uses the Keycloak Single Sign-on system, so you must first request a token from Keycloak using a username and password pair, before you make requests. By default this token will expire in 5 minutes, and will need to be refreshed if it expires before your next request."}),"\n",(0,t.jsx)(n.p,{children:"Keycloak Request:"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-bash",children:"# Get tokens from Keycloak - You will need to update the username and password with your account\ncurl -i -H \"Content-Type: application/x-www-form-urlencoded\" -d 'username=operator&password=operator&client_id=api&grant_type=password&scope=openid' -X POST http://127.0.0.1:2900/auth/realms/openc3/protocol/openid-connect/token\n"})}),"\n",(0,t.jsx)(n.p,{children:"Keycloak Response:"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-bash",children:'HTTP/1.1 200 OK\nCache-Control: no-store\nContent-Length: 3207\nContent-Type: application/json\nPragma: no-cache\nReferrer-Policy: no-referrer\nSet-Cookie: KEYCLOAK_LOCALE=; Version=1; Comment=Expiring cookie; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Max-Age=0; Path=/auth/realms/openc3/; HttpOnly\nSet-Cookie: KC_RESTART=; Version=1; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Max-Age=0; Path=/auth/realms/openc3/; HttpOnly\nStrict-Transport-Security: max-age=31536000; includeSubDomains\nX-Content-Type-Options: nosniff\nX-Frame-Options: SAMEORIGIN\nX-Xss-Protection: 1; mode=block\nDate: Wed, 10 May 2023 00:40:40 GMT\n\n{"access_token":"eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJ0cDlERmpNZGFXMy16WXptdlBqVTZnNTVqMVNhWGhkZHJqU0szQVNvaDhVIn0.eyJleHAiOjE2ODM2Nzk1NDAsImlhdCI6MTY4MzY3OTI0MCwianRpIjoiZmVlOTQwYWYtZDY3Ny00MWUyLWIzNWYtZDI5ODhiM2RhZGQ2IiwiaXNzIjoiaHR0cDovL2xvY2FsaG9zdDoyOTAwL2F1dGgvcmVhbG1zL29wZW5jMyIsInN1YiI6ImFjZTFlNmExLTkzMTktNDc2ZS1iZjQzLTZmM2NhYjllZTJkZSIsInR5cCI6IkJlYXJlciIsImF6cCI6ImFwaSIsInNlc3Npb25fc3RhdGUiOiJmMzc4NTk2Ny0yYTQ2LTRjMTItYWQwYy1jZmY3ZmM0NzdkZjkiLCJhY3IiOiIxIiwicmVhbG1fYWNjZXNzIjp7InJvbGVzIjpbImRlZmF1bHQtcm9sZXMtb3BlbmMzIiwiQUxMU0NPUEVTX19vcGVyYXRvciIsIm9mZmxpbmVfYWNjZXNzIiwiQUxMU0NPUEVTX192aWV3ZXIiXX0sInNjb3BlIjoib3BlbmlkIHByb2ZpbGUgZW1haWwiLCJzaWQiOiJmMzc4NTk2Ny0yYTQ2LTRjMTItYWQwYy1jZmY3ZmM0NzdkZjkiLCJlbWFpbF92Z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YtbvIA3DP8rgO6bVv0ogkjWtlda6MbkyZN-har8x3raUVSlUPRP2Basy1xSMNNA1jvB-nEM-aubrUZE6r0PjI6PE1hbLPmuPbcX3uuIwXu2-UoXepkB8H7omUuMm-S98aHpRarwszC0mmHD5b_wiXusMVw4xYw8eavFue4zfw-T2IKuTVtxbMTygXIah6iqi4gkpL8Mx1w","not-before-policy":0,"session_state":"f3785967-2a46-4c12-ad0c-cff7fc477df9","scope":"openid profile email"}\n'})}),"\n",(0,t.jsx)(n.p,{children:"COSMOS Request:"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-bash",children:'# COSMOS Request now looks like this:\n\ncurl -i -H "Content-Type: application/json-rpc" -H "Authorization: eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJ0cDlERmpNZGFXMy16WXptdlBqVTZnNTVqMVNhWGhkZHJqU0szQVNvaDhVIn0.eyJleHAiOjE2ODM2Nzk1NDAsImlhdCI6MTY4MzY3OTI0MCwianRpIjoiZmVlOTQwYWYtZDY3Ny00MWUyLWIzNWYtZDI5ODhiM2RhZGQ2IiwiaXNzIjoiaHR0cDovL2xvY2FsaG9zdDoyOTAwL2F1dGgvcmVhbG1zL29wZW5jMyIsInN1YiI6ImFjZTFlNmExLTkzMTktNDc2ZS1iZjQzLTZmM2NhYjllZTJkZSIsInR5cCI6IkJlYXJlciIsImF6cCI6ImFwaSIsInNlc3Npb25fc3RhdGUiOiJmMzc4NTk2Ny0yYTQ2LTRjMTItYWQwYy1jZmY3ZmM0NzdkZjkiLCJhY3IiOiIxIiwicmVhbG1fYWNjZXNzIjp7InJvbGVzIjpbImRlZmF1bHQtcm9sZXMtb3BlbmMzIiwiQUxMU0NPUEVTX19vcGVyYXRvciIsIm9mZmxpbmVfYWNjZXNzIiwiQUxMU0NPUEVTX192aWV3ZXIiXX0sInNjb3BlIjoib3BlbmlkIHByb2ZpbGUgZW1haWwiLCJzaWQiOiJmMzc4NTk2Ny0yYTQ2LTRjMTItYWQwYy1jZmY3ZmM0NzdkZjkiLCJlbWFpbF92ZXJpZmllZCI6ZmFsc2UsIm5hbWUiOiJUaGUgT3BlcmF0b3IiLCJwcmVmZXJyZWRfdXNlcm5hbWUiOiJvcGVyYXRvciIsImdpdmVuX25hbWUiOiJUaGUiLCJmYW1pbHlfbmFtZSI6Ik9wZXJhdG9yIn0.eSqSeZrmCTahwltz5jsu5r3w6W15T5h0BvIdqKWQBDcnxAcxKuT-Nwziw_ewySSgHeC172CIWJUpHVp8ACDQG-dfW4KkvA6AcGfSF_f8TBH_rZrVQwlvwwzdA_egGKzhZWcnAC8TDjXRxuaWmnOgWT0aaHZAoW8EvwmKp-1IVz2l0B-hqzfC7dkjMrCI1udLfDvDBza9OtuR-FnKGt8h4nYnRzr8pO2jwebPFyZlR00gVsyK-b411XqprpT-qpRObYZwH5womA-8xIiwRZj9dsfQ1TaHGFkp1LNzxcj_r6pfwVO263bohbeU7ImezQdbvGLJ9NHaglzVNroTui4BXA" -d \'{"jsonrpc": "2.0", "method": "tlm", "params": ["INST HEALTH_STATUS TEMP1"], "keyword_params": {"scope": "DEFAULT"}, "id": 8}\' -X POST http://127.0.0.1:2900/openc3-api/api\n'})}),"\n",(0,t.jsx)(n.p,{children:"COSMOS Response:"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-bash",children:'HTTP/1.1 200 OK\nCache-Control: max-age=0, private, must-revalidate\nContent-Type: application/json-rpc\nEtag: W/"1e44c0878528687014e1e60a1cbebdae"\nVary: Origin\nX-Request-Id: 47a8dd26-1348-4693-8df1-5375f60abc6c\nX-Runtime: 0.046477\nDate: Wed, 10 May 2023 00:41:33 GMT\nTransfer-Encoding: chunked\n\n{"jsonrpc":"2.0","id":8,"result":29.204100000000007}\n'})}),"\n",(0,t.jsx)(n.h2,{id:"suite-runner-example",children:"Suite Runner Example"}),"\n",(0,t.jsxs)(n.p,{children:["It can be very useful to run the a suite or script remotely from a continuous testing server. COSMOS' REST API allows for this. To figure out what is required to run a certain task on the web GUI you can open up your browser's developer tools to monitor the network traffic. You will see all the requests and responses required to run a command and you can reformat them yourself to suit your own purposes. Below is an example of running a test script from a Chromium-based browser:\n",(0,t.jsx)(n.img,{src:"https://github.com/OpenC3/cosmos/assets/55999897/df642d42-43e0-47f9-9b52-d42746d9746b",alt:"Network Traffic in browser developer tools"})]}),"\n",(0,t.jsxs)(n.p,{children:['You can see that there are 5 transactions total. To investigate just right-click on the network transaction and click "copy as ',(0,t.jsx)(n.code,{children:"curl"}),'" (depends on the browser). Here is an example of the second one:']}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-bash",children:"curl 'http://localhost:2900/script-api/scripts/TARGET/procedures/cmd_tlm_test.rb/lock?scope=DEFAULT' \\\n -X 'POST' \\\n -H 'Accept: application/json' \\\n -H 'Accept-Language: en-US,en;q=0.9' \\\n -H 'Authorization: pass' \\\n -H 'Connection: keep-alive' \\\n -H 'Content-Length: 0' \\\n -H 'Origin: http://ascportal:2900' \\\n -H 'Referer: http://localhost:2900/tools/scriptrunner/?file=TARGET%2Fprocedures%2Fcmd_tlm_test.rb' \\\n -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.0.0' \\\n --insecure\n"})}),"\n",(0,t.jsxs)(n.p,{children:["Many of the browser-specific headers are not required. The important thing to notice here is the URL and the request (in this case ",(0,t.jsx)(n.code,{children:"POST"}),"). If we inspect all of these we'll find out what each one does:"]}),"\n",(0,t.jsxs)(n.ol,{children:["\n",(0,t.jsxs)(n.li,{children:["Set the script contents","\n",(0,t.jsxs)(n.ul,{children:["\n",(0,t.jsx)(n.li,{children:"this updates any local changes)"}),"\n",(0,t.jsxs)(n.li,{children:["Note that this is a different request to ",(0,t.jsx)(n.code,{children:"GET"})," the script contents. This is done on the page load."]}),"\n"]}),"\n"]}),"\n",(0,t.jsx)(n.li,{children:"Lock the script (so other users can't edit it during execution)"}),"\n",(0,t.jsx)(n.li,{children:"Run script (this takes a JSON with options)"}),"\n",(0,t.jsx)(n.li,{children:"Open Websocket for logs"}),"\n",(0,t.jsx)(n.li,{children:"Request Result (this URL is a little different because the results are saved in redis)"}),"\n"]}),"\n",(0,t.jsxs)(n.p,{children:["Below is a bash script which does all the above given some options. It requires ",(0,t.jsx)(n.code,{children:"curl"})," for the web requests and ",(0,t.jsx)(n.code,{children:"jq"})," for JSON parsing and formatting. It locks and runs the script, continually checks its status, then requests the result."]}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-bash",children:'#!/bin/bash\nset -e\nTARGET=${1:-\'TARGET\'}\nSCRIPT=${2:-\'procedures/cmd_tlm_test.rb\'}\nSUITE=${3:-\'TestSuite\'}\nCOSMOS_HOST=\'http://localhost:2900\'\nSCRIPT_API="$COSMOS_HOST/script-api"\nSCRIPT_PATH="scripts/$TARGET/$SCRIPT"\nCURL_ARGS=( \n\t-H \'Accept: application/json\' \n\t-H \'Authorization: password\' \n\t-H \'Accept-Language: en-US,en;q=0.9\' \n\t-H \'Connection: keep-alive\' \n\t-H \'Content-Type: application/json\' \n\t--insecure \n\t--silent )\n\n# Lock script #\ncurl "$SCRIPT_API/$SCRIPT_PATH/lock?scope=DEFAULT" -X "POST" "${CURL_ARGS[@]}"\n\n# Run script #\nRUN_OPTS=$(cat <<-json\n{\n "environment": [],\n "suiteRunner": {\n "method": "start",\n "suite": "$SUITE",\n "options": [\n "continueAfterError"\n ]\n }\n}\njson\n)\nRUN_OPTS=$(<<<"$RUN_OPTS" jq -rc .)\nID=$(curl "$SCRIPT_API/$SCRIPT_PATH/run?scope=DEFAULT" --data-raw "$RUN_OPTS" "${CURL_ARGS[@]}")\n\necho "Starting Script \'$SCRIPT_PATH\' at $(date) (may take up to 15 minutes)" > /dev/stderr\necho "You can monitor it in Script Runner here: $COSMOS_HOST/tools/scriptrunner/$ID" > /dev/stderr\n# Loop while Script ID is still running #\nwhile true; do\n\tSCRIPT_STATUS="$(curl "$SCRIPT_API/running-script?scope=DEFAULT" "${CURL_ARGS[@]}" | jq ".[]|select(.id==$ID)")"\n\tif [[ -z $SCRIPT_STATUS ]]; then\n\t\tbreak;\n\tfi\n\tsleep 2\ndone\n\n# Request results #\nBUCKET_FILE_URI="$(curl "$SCRIPT_API/completed-scripts?scope=DEFAULT" "${CURL_ARGS[@]}" |\\\n\tjq \'[.[]|select(.name | test("\'"${SCRIPT_PATH#scripts/}"\' "))][0] | .log | @uri\' -r)"\n\nURL="$(curl "$COSMOS_HOST/openc3-api/storage/download/$BUCKET_FILE_URI?bucket=OPENC3_LOGS_BUCKET&scope=DEFAULT" "${CURL_ARGS[@]}" |jq .url -r)"\n\ncurl "$COSMOS_HOST$URL" "${CURL_ARGS[@]}"\n'})})]})}function d(e={}){const{wrapper:n}={...(0,s.R)(),...e.components};return n?(0,t.jsx)(n,{...e,children:(0,t.jsx)(p,{...e})}):p(e)}},1184:(e,n,i)=>{i.d(n,{R:()=>r});var t=i(4041);const s={},o=t.createContext(s);function r(e){const n=t.useContext(o);return t.useMemo((function(){return"function"==typeof e?e(n):{...n,...e}}),[n,e])}}}]);