Wonder why trying to upload a Letsencrypt .pem certificate using cURL to Cloudflare API throws "Malformed JSON in request body" error? - api

I have a script that attempts to replace my site's SSL cert on Cloudflare.
Certbot auto-renews the cert on the local server from Letsencrypt every three months. Now, the SSL .pem certificate renewed by certbot needs to be uploaded to Cloudflare using its API.
PRIVATE_KEY="/etc/letsencrypt/live/autoxxx.com.au/privkey.pem"
CERTIFICATE="/etc/letsencrypt/live/aautoxxx.com.au/cert.pem"
# read from files
PRIVATE_KEY=`cat $PRIVATE_KEY`
CERTIFICATE=`cat $CERTIFICATE`
DATA='{"private_key":"'$PRIVATE_KEY'","certificate":"'$CERTIFICATE'","bundle_method":"ubiquitous"}'
curl -i \
-X PATCH "https://api.cloudflare.com/client/v4/zones/rCWR4i3A24NZEzI4dFLYLAhU7tUBtJUSYQkh/custom_certificates/iqXVG2FV8Cgj5FXGMexIoJovtFQx5UhecVya" \
-H "X-Auth-Email: webdev#autoxxx.com.au" \
-H "X-Auth-Key: pg5Q89JI33nsgdA9iZwPky3q" \
-H "Content-Type: application/json" \
-d "$DATA" --trace-ascii /dev/stdout
But, running this script throws the following error
{"success":false,"errors":[{"code":6007,"message":"Malformed JSON in request body"}],"messages":[],"result":null}
Tried the quotes suggestion as in answer to the following question, but still the same error.
Why do I get a malformed JSON in request body in this cURL call?

I searched Google extensively.
Tried the following https://docs.vmware.com/en/Unified-Access-Gateway/3.0/com.vmware.access-point-30-deploy-config.doc/GUID-870AF51F-AB37-4D6C-B9F5-4BFEB18F11E9.html to put .pem into a single line. Used awk 'NF {sub(/\r/, ""); printf "%s\n",$0;}' to achieve this, but now it threw "Invalid Certificate" response.
The following code works perfectly. The above awk command was replacing carriage returns with new line \n, but Cloudflare, apparently, wants the literal "\n".
PRIVATE_KEY="/etc/letsencrypt/live/autoxxx.com.au/privkey.pem"
CERTIFICATE="/etc/letsencrypt/live/aautoxxx.com.au/cert.pem"
# read from file, put the .pem into single line and replace carriage returns with the literal "\n"
PRIVATE_KEY=`awk 'NF {sub(/\r/, ""); printf "%s\\\n",$0;}' $PRIVATE_KEY`
CERTIFICATE=`awk 'NF {sub(/\r/, ""); printf "%s\\\n",$0;}' $CERTIFICATE`
DATA='{"private_key":"'$PRIVATE_KEY'","certificate":"'$CERTIFICATE'","bundle_method":"ubiquitous"}'
curl -i \
-X PATCH "https://api.cloudflare.com/client/v4/zones/rCWR4i3A24NZEzI4dFLYLAhU7tUBtJUSYQkh/custom_certificates/iqXVG2FV8Cgj5FXGMexIoJovtFQx5UhecVya" \
-H "X-Auth-Email: webdev#autoxxx.com.au" \
-H "X-Auth-Key: pg5Q89JI33nsgdA9iZwPky3q" \
-H "Content-Type: application/json" \
-d "$DATA" --trace-ascii /dev/stdout

Related

Spotify curL invalid client issue

I'm following the source here, but I can't send a query with curL. I am getting an invalid client error.
curl -H "Authorization: Basic <base64 ZGQyXGNlZQY1OTUxNDc3NGJhMm.......ZTU0YDY=>" -d grant_type=authorization_code -d code=code -d redirect_uri=http%3A%2F%2Flocalhost:3000 https://accounts.spotify.com/api/token
According to the source, I need to get my refresh token but somehow I couldn't.
Unfortunately I couldn't do it and I would be very grateful if you could help.
CurL query below worked
Get the refresh token
curl -d client_id=$CLIENT_ID -d client_secret=$CLIENT_SECRET -d grant_type=authorization_code -d code=$CODE -d redirect_uri=$REDIRECT_URI https://accounts.spotify.com/api/token
Thanks,

Newman: How to send ssl cert in newman request

I have a curl command that I'd like to have run as a collection using newman:
curl -i -v \
-H "X-FromAppId: APPLICATION" \
-H "X-TransactionId: 22222222" \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
--cacert iis_cert_2018_07.pem \
--key iis_cert_2018_07.pem \
--cert iis_cert_2018_07.pem \
-X GET https://iis-udev02.dev.test.com:8443/iis/v1/common/policies/343
--insecure
I have a collection that executes that GET request, and it works just fine on Postman (after configuring the certs in the UI), but how do I notify newman of those certs when running the collection through the commandline?
I've tried:
newman run GETRequest.postman_collection.json --ssl-client-cert iis_cert_2018_07.pem --ssl-client-passphrase iis_cert_2018_07.pem --insecure
But that doesn't work.
How is a cert sent in a newman request?
You are passing in the key as the passphrase here: --ssl-client-passphrase iis_cert_2018_07.pem, it should be --ssl-client-key iis_cert_2018_07.pem
Try:
newman run GETRequest.postman_collection.json --ssl-client-cert iis_cert_2018_07.pem --ssl-client-key iis_cert_2018_07.pem --insecure
This should work assuming the key is not encrypted with a passphrase, and everything else is right.
The relevant newman documentation is here:
https://www.npmjs.com/package/newman#ssl-client-certificates
if you have the key encrypted then you can use the below command
newman run GETRequest.postman_collection.json --ssl-client-cert iis_cert_2018_07.pem --ssl-client-key iis_cert_2018_07key.pem --ssl-client-passphrase password

Spring Cloud config server encryption decryption

I am using the below command to encryot the password.
curl -H "Content-Type: text/plain" lonrs03668:8888/encrypt -d Simple12$jedi
after decryting the password, the value after $ sign getting lost and it is only returning the Simple12.
The issue is that if string contains the $ sign it is not encrypting/decrypting correctly.
Try curl -H 'content-type: text/plain' localhost:8888/encrypt --data-raw 'your password'
Below is working for me by adding \ before $ .
curl -u root:s3cr3t -H "Content-Type: text/plain" lonrs03668:8888/encrypt --data-ascii Simple12\$jedi.

Curl command no return

Im working with an api, in the documentaion found here:
http://api.simplicate.nl/
There is an example curl:
`curl -H “Authentication-Key: {API Key}” -H “Authentication-Secret:{API secret}” https://{subdomain}.simplicate.nl/api/v2/crm/organization.json`
I ran that code like this in terminal:
curl -H “Authentication-Key:XX” -H “Authentication-Secret:XX” https://mydomain.simplicate.nl/api/v2/crm/organization.json
It runs but returns nothing.
You are using Header inside “...” that is wrong. You have to use double quote "..." (not sure what it is called, standard double quote?).
So it should be:
curl -H "Authentication-Key:XX" -H "Authentication-Secret:XX" https://mydomain.simplicate.nl/api/v2/crm/organization.json
As a note, currently your curl is sending the headers as following with extra characters.
“Authentication-Key:XX”
“Authentication-Secret:XX”
But it should be:
Authentication-Key:XX
Authentication-Secret:XX

curl -h or curl --header not working

i'm trying to connect ti a web api service, box-api, and following the tutorial i have to type this command to fetch a folder in the user content:
curl https://api.box.com/2.0/folders/FOLDER_ID/items?limit=2&offset=0 -H "Authorization: Bearer ACCESS_TOKEN"
I tryied to connect from the command line to test the command but it keep complaining about the -H or the --header command saying that it doesn exist:
-bash: -H: command not found
-bash: --header: command not found
but when i type curl --help the command is in the manual:
-H, --header LINE Custom header to pass to server (H)
I'm confused, what should i do to connect to this site and get the JSON content? Thanks
Your url has & sign. and this is making end of command on there(and running at background). You can remove this error by using quotes around. Like this
curl "https://api.box.com/2.0/folders/FOLDER_ID/items?limit=2&offset=0" -H "Authorization: Bearer ACCESS_TOKEN"
Hope this helps.
There seem to be two problems:
The '&' in the middle of the URL passed to curl,
The order of the statements. The curl manual reports a different order for the statements. Example:
curl -H "Authorization: Bearer AUTH_KEY" "https://api.box.com/2.0/folders/0/items?limit=2&offset=0"
This should be the complete solution.