How to urlencode url params with a curl POST but not urlencode the body data? - shell

When making a curl POST call with something like ... | curl -XPOST --data #- https://example.com/ can I also url-encode some url params on the command line (using curl itself, not bash funcs) without having to encode them myself and put them in the url argument to curl?
So I want the POST Body to not be urlencoded, but the url params to be url encoded within the same curl command.
So for example:
echo '{"username":"a","password": [1,2]}' | curl --header "Content-Type: application/json" --request POST -d#- --data-urlencode "filter=." http://localhost:8080
Doesn't seem to do what I want, and I have postfix my https://localhost:8080/ url with ?filter... which I would rather avoid for cases when the param needs a lot of encoding.

You can use --data-urlencode option:
... | curl -s -X POST --data #- --data-urlencode "filter=ab&123" <url>
As per man curl:
--data-urlencode <data>
(HTTP) This posts data, similar to the other -d, --data options with the exception that this performs URL-encoding.

Related

Cannot import Jelastic Manifest with cURL command using two -H parameters

I'm trying to create a Jelastic Manifest with a cURL command inside it. When I import it, it gives me an error but unfortunately I have no access to the console (disabled by the provider).
The command is the following :
curl -X POST <my_url> -H "Content-Type: application/json" -H "Authorization: Bearer <token>" -d "{}"
Some additionnal information :
The URL is correct 100%
The token does not contain any special characters : Only upper/lowercase characters and numbers
The command is run successfully from the command line
If I remove the first -H parameter, I can import my manifest. Same if I remove the second -H parameter and keep the first one
My guess is that, somehow, having two -H is not considered as valid but I don't know why. Any ideas ?
EDIT : A screenshot of the error shown on the platform
The "two -H parameters" wasn't a root cause in your question.
The thing is that the YAML gets the data you sent as a key/value array (dictionary).
In your example it would be:
curl -s -X POST https://test.com -H "Content-Type - as a key,
and
application/json" -H "Authorization: Bearer xx" -d "{\"Key\":\"Value\"}" - as a value.
If you need an array of strings the YAML may be as this
cmd [cp]:
- 'curl -s -X POST https://test.com -H "Content-Type: application/json" -H "Authorization: Bearer xx" -d "{\"Key\":\"Value\"}"'
If you need a multi-line string it should look like this
cmd [cp]: |
curl -s -X POST https://test.com -H "Content-Type: application/json" -H "Authorization: Bearer xx" -d "{\"Key\":\"Value\"}"

bash script to convert string value to json and then return json

I am very new to bash scripting, please can someone point me in the right direction on how to accomplish my task?
I have a curl call which returns a string, which I want to convert to json.
My curl statement -
curl --insecure -X POST 'https://url/api/IPAM/GetIP' --header 'Content-Type: application/json' --header -d '{"key1": "value1"}'
This curl statement returns a string ,for example: 10.100.100.100
I want to fetch this string and return the output in json format:
{"IP":"10.100.100.100"}
I don't want to use jquery or python to do this because this entire script will be run by a wrapper that only understands bash.
You can use jq to process your IP string into a JSON string and package it into a JSON object of your choice.
ip="10.100.100.100"
jq --arg ip "$ip" -cn '{"IP":$ip}'
Result:
{"IP":"10.100.100.100"}
Now if working with the result of your example curl POST request:
rawip_string=$(curl --insecure -X POST 'https://url/api/IPAM/GetIP' --header 'Content-Type: application/json' --header -d '{"key1": "value1"}')
jq --arg ip "$rawip_string" -cn '{"IP":$ip}'
One way to not rely on external tools like jq, you can get the output ip attribute that to a variable and concatenate it.
$ return=$(echo 10.100.100.100)
$ echo "{\"IP\":\"${return}\"}"
{"IP":"10.100.100.100"}
Like this
printf '{"IP":"%s"}' "$(curl --insecure -X POST 'https://url/api/IPAM/GetIP' --header 'Content-Type: application/json' --header -d '{"key1": "value1"}')"

Redirect a cURL response to a cURL that POSTs, but not through a file

I 'd like to post directly a json object from a url(json) to another url
so the command goes as follows:
curl "<resource_link>.json" -o sample.json
curl -X POST "<my_link>" "Content-type: application/json" -d #sample.json
I 'd like to avoid this, so what is the solution? Is it something like that?
curl -X POST "<my_link>" "Content-type: application/json" -d "curl <resource_link>.json"
But it does not work? Also, this one post Stream cURL response to another cURL command posting the result
does not explain thouroughly and it is not working
Yes,
curl
manual explains the '#' but it does not explain about using another curl
Alternatievely, if I could save somewhere temporarily the 1st cURL response and use it in the other command(but not in a file)
You don't want -x POST in there so let's start with dropping that.
Send the results from the first transfer to stdout by not using -o, or telling -o to use stdout with -o-, and
Make sure your second transfer accepts the data to send on stdin, by using -d#-.
curl "<link>.json" | curl "<link2>" -H "Content-type: application/json" -d #-
With curl 7.82.0 and later
Starting with curl 7.82.0 you can do it even easier with the new --json option:
curl "<link>.json" | curl "<link2>" --json #-

How to pass object as a query param in spring data REST

I am having a EmailRepository which queries for a person like follows
findByPerson(Person person);
The spring-data-rest /search resource list the following href
http://localhost:8080/emailAddresses{?person}
How to make a curl command for the above search url in GET method? Also if the person does not exists, I need to throw "PersonNotFoundException".
I am using org.springframework.boot:spring-boot-starter-data-rest:jar:1.3.3.RELEASE
Try the below curl commands -
if you are using path param -
curl -i -X GET -H "Content-Type: application/json" -H "Cache-Control: no-cache" -k "http://localhost:8080/emailAddresses/person"
if you are using query param -
curl -i -X GET -H "Content-Type: application/json" -H "Cache-Control: no-cache" -k "http://localhost:8080/emailAddresses?person=aaa"

How to insert data into HBase through StarGate REST API

According to the StarGate documentation, this is how a CURL command should look like:
% curl -H "Content-Type: text/xml" --data '[...]' http://localhost:8000/test/testrow/test:testcolumn
This is what I'm trying:
% curl -X POST -H "Accept: text/xml" --data '[<CellSet><Row key="cm93MQ=="><Cell column="dGl0bGU6YQ==">d29ya2Vk</Cell></Row></CellSet>]' http://localhost:8080/test/row1/title
Keep getting HTTP 415, Unsupported Media Type.. any ideas what I'm missing there?
Your current curl options specify you'd like XML output, and curl is assuming you are posting url-encoded form data (and specifying the wrong Content-Type in the HTTP headers).
Amend your -H "Accept: text/xml" to -H "Content-Type: text/xml" and you should be good

Resources