2

I am new to shell script.

I am trying to use one curl request like this in my shell script.

curl -X POST --header "Content-Type: application/json" --header "Accept: */*" "http://localhost:8080/api/rest/v1/category/p1/{id}?id=${a}&name=${b}&typecode=${c}

where $a or $b or $c may contains words seperated by spaces due to which curl request is getting failed.

2 Answers 2

2

You should also be able to get curl to do the work of encoding space and other special characters for you in the query string by using --data-urlencode, whilst adding -G to make it part of the url:

curl --header "Content-Type: application/json" --header "Accept: */*" \
"http://localhost:8080/api/rest/v1/category/p1/{id}" \
--data-urlencode id="${a}" \
--data-urlencode name="${b}" \
--data-urlencode typecode="${c}" \
-G -X POST

Test by setting a='a a' b='b b' c='a&b&c' and adding -v. You get the header:

POST /api/rest/v1/category/p1/id?id=a%20a&name=b%20b&typecode=a%26b%26c HTTP/1.1

By the way, {} should be encoded in urls so curl has removed them.

Sign up to request clarification or add additional context in comments.

Comments

1

You can use:

curl -X POST --header 'Content-Type: application/json' --header 'Accept: */*' \
 "http://localhost:8080/api/rest/v1/category/p1/{id}?id=${a// /%20}&name=${b// /%20}&typecode=${c// /%20}"

${a// /%20} (an similarly for $b and $c) will replace all space by %20 (encoded space) so that your rest API gets all parameter values correctly.

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.