In a bash script i want to get date dynamically and send it along with the
curl call.
i have got the date from the user in the bash script
and in the script am making the below curl call. am already passing the request params using a separate file as below. How do i pass the date?
i have tried like $date, but it is not working, even tried "'$date'".
The below is my curl call:
curl -O -X POST -H "Content-Type: application/json" -d#formparams.json --url http://test.com
Contents of form params json: it has more than 10 params for simplicity iam including only two
{"params":"{"HOSTS:":"1",date=$date}}
in the above i have added date.
But the date is not replaced.
Any help is appreciated.
Use a here document instead of a separate file for the parameters. Inside the here document, you can run the date command in a command substitution to provide the correct date when the document is read.
curl -O -X POST -H "Content-Type: application/json" -d#- --url http://test.com <<EOF
{"params":"{"HOSTS:":"1", "date": "$(date)"}}
EOF
use this json for pass dynamic parameter
{"params":"{"HOSTS:":"1",date="'$date'"}}
Related
I'm trying to generate json file using curl and also assign specific path where in the json file will store once generated, but I tried some commands but no json output.
May I know what I need to add or change with my command?
curl -v -H "Accept: application/json" --user "admin:Test1234" https://test.com/adventure/
I test a simple curl for a json public api and send de response to a file and the result is a JSON output.
curl -H "Accept: application/json" https://catfact.ninja/fact >> cat.json
You can try it using https://reqbin.com/req/javascript/c-vdhoummp/curl-get-json-example
Or simply using postman and see the code snippet option to check cUrl code snippet.
https://imgur.com/a/LXqN8YH
I'm able to generate JSON file. I add -k on my command since my URL is HTTPS.
I am trying to apply a long JSON via curl POST, but fails probably due to syntax. The script also contains multiple arrays in order to fulfil the script variables.
When I run the script in order to print (echo) the JSON, it prints successfully the JSON, which is also validated.
When I run the script in order to apply the JSON (curl -X POST), it fails.
Is there any other option in order to apply the JSON without modifying it?
thank you.
You can add -H "Content-Type: application/json" header value to Post the JSON data to curl command line.
For example :
curl -X POST -H "Content-Type: application/json" -d '{"username":"abc","password":"abc"}' https://api.xyz.com/v2/login
Sorry Pretty noob to json.
Basically I have a simple server where I can upload data in there.
E.g:
curl -vX PUT "http://IP:port/ABC" -H "Content-Type: application/json" -d #"Once Upon a time."
After when I do:
curl -vX GET "http://IP:port/ABC" -H "Content-Type: application/json"
I get:
{"reverse_shell":
{"aliases":{},"mappings":{},"settings":
{"index":{"creation_date":"1561863982371","number_of_shards":"5","number_of_replicas":"1","uuid":"IAWE83rYQqmtKW-9svkBVg","version":{"created":"6040299"},"provided_name":"ABC"}
}
}
}
As you can see there is no where mentioning Once Upon a time, so is there I am missing? or how do I get that data from json using curl?
I am in kali linux env.
It looks like you are trying to post a string as a file.
When you specify a "#" with -d this tells curl to send the data from a file called "Once Upon a time."
If you are trying to put a file then you should do:
my_text_file.txt
Once Upon a time.
curl -vX PUT "http://IP:port/ABC" -H "Content-Type: application/json" -d #./my_text_file.txt https://server/api/path
I want to execute a bash script after i make a POST request.So far i am using Postman for sending the request , but i was wondering if i can somehow do it from a bash script as well with a json file as parameter.
I have looked into curl so far but it does not work:
bash file
curl -X POST -d req.json http://localhost:9500
Json file (req.json)
{
"id":5,
"name":"Dan",
"age":33,
"cnp":33,
"children":100,
"isMarried":0
}
I just get the error :
HTTP/1.0 503 Service Unavailable
with the trailing HTML
curl should do the job. This will send a normal POST request using the data in req.json as the body:
curl -X POST -H "Content-Type: application/json" -d #req.json http://localhost:9500
The elements you were missing are -H "Content-Type: application/json" and the # in the data flag. Without the -H flag as above curl will send a content type of application/x-www-form-urlencoded, which most applications won't accept if they expect JSON. The # in the -d flag informs curl that you are passing a file name; otherwise it uses the text itself (i.e. "req.json") as the data.
Can someone plz tell me, when I launch just curl with POST some json data it works, but when I'm trying to add variable in json and cycle for, bash returns invalid_json.
I've read that bash variables are untyped so why I got this error?
#!/bin/bash
for var in 14456 14455
do
curl -X POST -d '{"api_token": "test_api", "id": $var}' 'https://api_test.com/'
done
you need double quotes to do variable interpolation in bash:
curl -X POST -d "{\"api_token\": \"test_api\", \"id\": $var}" 'https://api_test.com/'