1

I have the following cURL request. I want to get the http_code, but I want in a different variable, because otherwise it messes with parsing the JSON response from the GET call.

Is there anyway to do this?

curl --write-out %{http_code} --silent --output GET --header "Accept: application/json" --header "URL"

5 Answers 5

2

Just use command substitution to store status code in a variable:

status=$(curl --write-out %{http_code} --silent --output tmp.out GET --header "Accept: application/json" --header "URL")
data=$(<tmp.out)

# check status now
declare -p status

# check data
declare -p data
Sign up to request clarification or add additional context in comments.

7 Comments

I also need the actual output though. Does this give me both the output and the status code?
This just appends the status code to the end of the JSON response.
But you have --output /dev/null which is suppressing all the output.
My bad! That was a typo. Lemme edit that in the question. Any suggestions now?
@anubhava: This is what I see as output declare -- status="000<JSON_RESPONSE>200" declare -- data="" Not exactly what I wanted, but I think you're close?
|
0

curl -i -H "Accept: application/json" "server:5050/a/c/getName{"param0":"Arvind "}"

1 Comment

This is just functioning as a regular curl request.
0

curl -w 'RESP_CODE:%{response_code}' -s -X POST --data '{"asda":"asd"}' http://example.com --header "Content-Type:application/json"|grep -o 'RESP_CODE:[1-4][0-9][0-9]'

1 Comment

I need both the response code AND the actual JSON response
0

response=$(curl -sb -H "Accept: application/json" "http://host:8080/some/resource") For response just try $response

Comments

0

Try this following may be this could help you to find the solution https://gist.github.com/sgykfjsm/1dd9a8eee1f70a7068c9

Comments

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.