Prevent cURL from stripping newlines when getting data from the API

flyboy
Contributor

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
1 ACCEPTED SOLUTION

mm2270
Legendary Contributor III

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"

View solution in original post

2 REPLIES 2

mm2270
Legendary Contributor III

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"

flyboy
Contributor

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.