How to GET data of a key in json using curl? - json

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

Related

base64 conversion in context with curl

I would like to send a message and attachment via signal-cli.
I successfully set up docker a container by bbernhard/signal-cli-rest-api.
Normal message sending with curl-statement works fine and statement looks like:
curl -X POST -H "Content-Type: application/json" -d '{\"message\": \"Hello World!\", \"number\": \"+490000000\", \"recipients\": [\"+4900000000"]}' 'http://localhost:48080/v2/send'
The message will be sent to one recipient or many. Also working for a group by groupID.
Question: How to add an attachment like a JPG?
If I add \"base64_attachments\": [\"${ENCODED_IMAGE}")\"] to the statement, then i get the error message {"error":"Couldn't process request - invalid request"}
Full bash script looks like :
#!/bin/bash
INPUT_FILE="/path/to/file/IMG_5098.JPG"
TMPFILE=$(mktemp)
base64 "${INPUT_FILE}" --wrap=0 > "${TMPFILE}"
ENCODED_IMAGE=$(cat "${TMPFILE}")
curl -X POST -H "Content-Type: application/json" -d '{\"message\": \"Hello World!\", \"base64_attachments\": [\"${ENCODED_IMAGE}")\"], \"number\": \"+4900000\", \"recipients\": [\"+4900000000\"]}' 'http://localhost:48080/v2/send'
rm "${TMPFILE}"
I expected the image send as well as the message

Apply JSON format via Curl POST

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

Sending POST Request from bash script

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.

Have an R Plumber API consume JSON on POST

I'm writing and API in R using plumber that ideally will consume the JSON it receives on POST. But I cannot get the endpoint POST example to work that way, so I'm probably missing something obvious.
Using the example URL and Curl I can do the following without issue:
curl -i -X POST http://plumber.tres.tl/append/append -d "val=50"
But the way the example is presented:
POST {val: 50} -> http://plumber.tres.tl/append/append
Suggests that JSON would also be allowed. So I have tried:
curl -H "Content-Type: application/json" -X POST -d '{"val":50}' http://plumber.tres.tl/append/append
And all the variation to ensure UTF-8 encoding, comment out the " and all kinds of other combinations based mostly on what I found here on Stackoverflow about post. For example:
curl -i -X POST -H "Content-Type: application/json" http://plumber.tres.tl/append/append -d '{"val":50}'
curl -i -X POST -H "Accept: application/json" -H "Content-Type: application/json" http://plumber.tres.tl/append/append -d '{\"val\":50}'
curl -i -X POST -H "Content-Type: application/json;charset=UTF-8" http://plumber.tres.tl/append/append -d '{"val":50}'
Also using a file and trying to post it as #my.json did not work.
Maybe it is something on the Plumber side: I would expect that given the toolset to serialize the output, I can also state the expected serialization of the input. But I have not found how to do that.
This turned out to be a relatively simple issue with plumber. The function postBodyFilter calls parseQS that in turn splits on & and = and does not yet check for JSON formats (for example based on an initial { and ending }).
Since jsonlite was already imported by the package I proposed a small change to add basic JSON support in pull request #53.
Following the example in the README, the following will work after adding this patch:
curl --data '{"a":4, "b":5}' http://localhost:8000/sum
Since the call is on jsonlite to parse the content of the querystring, more complex JSON should also be possible, but I have not tested that yet.
Update : This has now been merged into the plumber project and will work for you if you install the version from github using devtools::install_github("trestletech/plumber"), or through a traditional install as soon as version 0.3.1 is available on CRAN.

pushbullet api decoding (v2/push)

I want to use the pushbullet api (v2/push) for pushing messages, but if I include '%' character inside title or body the server gives me the following error:
{"error":{"type":"invalid_request","message":"Failed to decode urlencoded POST form body.","cat":"~(=^‥^)ノ"}}
How can I fix this problem?
request: curl https://api.pushbullet.com/v2/pushes -k -u token: -d type=note -d title="%test" -d body="%%test" -X POST
x-www-form-urlencoded is not the most straightforward of formats. You can probably use curl with the --data-urlencode option. You can also try encoding your values with this tool: http://meyerweb.com/eric/tools/dencoder/
That should produce urlencoded output, for instance your request would look more like:
curl -u token: https://api.pushbullet.com/v2/pushes --header "Content-Type: application/x-www-form-urlencoded" --data-binary 'type=note&title=TITLETEXT&body=%25BODYTEXT'