Posted on 06-10-2015 08:09 AM
I'm working on a bash script that will run arbitrary API queries and then write the output to a file. At present, I'm using cURL to send commands to the JSS. This works. The only issue i'm having is that the returned data has no newline characters in it, and is one long line containing the XML data. I've read through the man page for cURL and the only place it mentions preserving newlines is for PUT commands. This is all well and good except for the fact that I'm only interested in GET for this project.
Does anyone know how I can either get cURL to preserve newlines in data received from a GET command, or know of a better tool to use in a bash script? Is something like python, or another language a better way to go? My current script is below for reference.
Thanks in advance!
#!/bin/bash
set -o nounset # Treat unset variables as an error
server="devjss.mycompany.com"
apiBaseURL="https://$server:8443/JSSResource"
apiURLSuffix=$1
apiUser="username"
apiPass="password"
curlCMD="-X GET --noproxy $server -s -u $apiUser:$apiPass $apiBaseURL/$apiURLSuffix"
outfile=~/Desktop/jssReport.xml
echo `curl $curlCMD` > $outfile
Solved! Go to Solution.
Posted on 06-10-2015 08:20 AM
Not 100% certain I understand your question, but if I'm reading it right, you're saying you want the xml output pulled with curl to respect the xml breaks, so its not just a bunch of xml tags running together, correct?
If so, pipe the xml output through xmllint to format as an actual xml file. That's generally how I do it when working with API data. Not certain if this will exactly work due to how you have your script set up, but this is the general idea anyway.
echo `curl $curlCMD` | xmllint --format - > $outfile
BTW, I suggest always adding a header to your curl command. As of some version 9.x of the JSS, we started getting API output defaulting to JSON. Adding a header to force it to xml (assuming you always want xml) migth be a good idea as a just in case.
-H "Accept: application/xml"
Posted on 06-10-2015 08:20 AM
Not 100% certain I understand your question, but if I'm reading it right, you're saying you want the xml output pulled with curl to respect the xml breaks, so its not just a bunch of xml tags running together, correct?
If so, pipe the xml output through xmllint to format as an actual xml file. That's generally how I do it when working with API data. Not certain if this will exactly work due to how you have your script set up, but this is the general idea anyway.
echo `curl $curlCMD` | xmllint --format - > $outfile
BTW, I suggest always adding a header to your curl command. As of some version 9.x of the JSS, we started getting API output defaulting to JSON. Adding a header to force it to xml (assuming you always want xml) migth be a good idea as a just in case.
-H "Accept: application/xml"
Posted on 06-10-2015 08:50 AM
Thanks @mm2270! The xmllint --format is the piece that I've been missing! I remember seeing the post a while back about getting JSON instead of XML data back, I'll be sure to add the header to my requests from now on.