-1

I want to parse below HTTP response but I cannot figure out how to grep the values separately using a single curl request.

I need these two output. 1. status_code - (Array of HTTP status codes) 2. exceptionMsg - (Occured exception in a variable)

 HTTP/1.1 100 Continue

    HTTP/1.1 400 Bad Request
    Content-Type: application/json; charset=utf-8
    Content-Length: 173
    Connection: close

    {"RemoteException":{"exception":"IllegalArgumentException","javaClassName":"java.lang.IllegalArgumentException","message":"Failed to parse \"false?op=CREATE\" to Boolean."}}

I tried this

curl -i  -X PUT -T test1.txt "http request"| grep HTPP
curl -i  -X PUT -T test1.txt "http request" | grep Exception

How can I get this done in one command ?

5
  • what do you mean by array of status codes? Commented Jan 15, 2016 at 5:59
  • There will be more than one status code i want all of them..I might have used wrong term to explain it Commented Jan 15, 2016 at 6:01
  • where? in the HTTP response or in the JSON? the first line looks ok just a typo - you misspelled HTTP Commented Jan 15, 2016 at 6:03
  • the HTTP response will only have one status code per request. do you want to save the status code from each request into an array? Commented Jan 15, 2016 at 6:04
  • I'm getting two status codes/request I want both in one array Commented Jan 15, 2016 at 6:08

2 Answers 2

2

grep supports regular expressions. Example:

curl -i  -X PUT -T test1.txt "http request"|  grep -E "(HTTP|Exception)"
0

You could try saving the curl output to a temporary file, get your status codes then delete file.

...
# put response in a temporary file
curl -i http://www.example.com -o response.html

# initialise array to hold status_codes in
status_codes=()

status_codes+=$(sed -n "1p" response.html | grep -o "[[:digit:]]\{3\}")
status_codes+=($(sed -n "3p" response.html | grep -o "[[:digit:]]\{3\}"))
#                       ^ change this to the line numnber where the reponse code is

# print status_codes array (you probably want to comment this out)
declare -p status_codes

rm response.html

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .