1

within my bash script I have a line that looks like this:

JOB_RESULTS=$(curl --fail -k -H "Content-Type: application/xml" --write-out HTTP_CODE='%{http_code}' "${request_url}")

This line outputs the following:

{"name":"callCLF010Job","id":11693,"status":"STARTING","message":"The job has started.","exitCode":"exitCode=UNKNOWN;exitDescription="}HTTP_CODE=200

The problem is, I want to add the HTTP_CODE into the JSON message.

Is there a way to make the return look like {"name":"callCLF010Job","id":11693,"status":"STARTING","message":"The job has started.","exitCode":"exitCode=UNKNOWN;exitDescription=","HTTP_CODE":"200"}

EDIT:

With confettis changes my code looks like this:

http_code="${JOB_RESULTS:${#JOB_RESULTS}-17}"
http_body="${JOB_RESULTS:0:${#JOB_RESULTS}-17}"
http_code_json=", ${http_code}}"
my_result="${http_body/%\}/$http_code_json}"

when I run echo $my_result my output looks like this:

{"name":"callCLF010Job","id":11702,"status":"STARTING","message":"The job has started.","exitCode":"exitCode=UNKNOWN;exitDescriptio

2 Answers 2

1

One option is to use sed to format the output the way you want:

echo $JOB_RESULTS | sed 's/}HTTP_CODE=\([0-9]\+\)$/,"HTTP_CODE":"\1"}/'

Or inside your command:

JOB_RESULTS=$(curl --fail -k -H "Content-Type: application/xml" --write-out HTTP_CODE='%{http_code}' "${request_url}" | sed 's/}HTTP_CODE=\([0-9]\+\)$/,"HTTP_CODE":"\1"}/')
Sign up to request clarification or add additional context in comments.

4 Comments

I want to be able to use jq to grab the code, for example: http_status=$(echo "${JOB_RESULTS}" | jq -r '.HTTP_CODE')
I see that this formats the output, but what I want is to actually insert HTTP_CODE into the $JOB_RESULTS, so that if I echo'd just $JOB_RESULTS HTTP_CODE would appear there
In this case you can use sed inside your command. I edited the answer to show you.
Thank you so much @Murilo Dutra, that was very simple and exactly what I needed
1

This is using shell parameter expansion to get the job done. (Pure bash, besides curl.)

curlout=$(curl -s --fail -k -H "Content-Type: application/xml" --write-out '"HTTP_CODE":"%{http_code}"' "http://example.com")
http_code="${curlout:${#curlout}-17}"
http_body="${curlout:0:${#curlout}-17}"
http_code_json=", ${http_code}}"
my_result="${http_body/%\}/$http_code_json}"

Replace http://example.com with the URL or variable of your choice.

In order to prevent errors in case of a wrong URL or (no) output, you should put the last four lines within an if-construct. E.g. if [[ $http_code != '"HTTP_CODE":"000"' ]].

11 Comments

Are you sure this will work? my_result is missing some quotations and brackets, I'm not sure how you want this formatted. I want to be able to use jq to parse HTTP_CODE from the response
@Josh I'm sorry, I've had two stupid typos in my answer. I've edited it, please try it again now. echo $my_result | jq .HTTP_CODE does now print "200" for me. If you need a numeric value let me know then I can edit my answer accordingly.
I'm not sure what you edited, http_code_json seems to have an extra bracket, and for some reason the -17 in http_body is red highlighted in my vim syntax
I'll keep trying for now, you seem very close to what I'm looking for Thank you
@Josh I've had an = in my original answer for the JSON when it has to be a :. I tried my answer with http://a.4cdn.org/boards.json instead of http://example.com and it works flawlessly.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.