Elasticsearch API insert data from path - json

The following cURL command is working:
curl -XPOST http://localhost:9200/test/test -d '{"name":"John", "age":31}' -H 'Content-Type: application/json'
However, when I execute the following command:
curl -XPOST http://localhost:9200/test/test -d '/mnt/c/path/to/filename/test.json' -H 'Content-Type: application/json'
I receive the following error:
{"error":{"root_cause":[{"type":"mapper_parsing_exception","reason":"failed to parse"}],"type":"mapper_parsing_exception","reason":"failed to parse","caused_by":{"type":"not_x_content_exception","reason":"Compressor detection can only be called on some xcontent bytes or compressed xcontent bytes"}},"status":400}
This is kind of weird since the content of this 'test.json' is exactly the same as in the first command. Namely: A compressed version of: '{"name":"John", "age":31}'.

When sending a file you need to use the --data-binary flag instead of -d:
curl -XPOST http://localhost:9200/test/test --data-binary #'/mnt/c/path/to/filename/test.json' -H 'Content-Type: application/json'

Related

How to return the full view of the data while using rest api

There is POST here which is calling a rest API to fetch the data.
Below is Just the Short code using rest API Call to fetch the data using /rest/resource-alerts, but it only displays max 500 Critical alerts not all. Below is the Code I'm using:
sessionID=$(curl -k -H "accept: application/json" -H "content-type: application/json" -H "x-api-version: 120" -d '{"userName":"administrator","password":"123456"}' -X POST https://synergy.ksg.com/rest/login-sessions | jq -r ".sessionID")
# cUrl command with header and payload information to ftech the Data using API call
CMD=$(curl -k -H 'accept: application/json' \
-H 'content-type: text/csv' \
-H 'x-api-version: 2' \
-H "auth: $sessionID" \
-X
GET https://synergy.ksg.com/rest/resource-alerts)
echo "$CMD"
However, while today going via API DOC I see the Query Parameters , The section says about the count, does anyone know how to get the full list of messages?
Below is the

Convert cURL To Applescript

I am trying to convert a cURL command to AppleScript Using "do shell script".
I've used various hints from previous posts but I'm still running into errors.
Can anyone point out errors in my syntax.
The below script works fine when run in terminal.
curl -X POST https://content.dropboxapi.com/2/files/upload \
--header 'Authorization: Bearer MYTOKEN' \
--header 'Content-Type: application/octet-stream' \
--header 'Dropbox-API-Arg: {"path":"/RoomVT/Clown2.jpeg"}'
--data-binary #"/Splash.jpeg"
I then adapt as.
--Add do shell script Command--
--Place The cURL Inside Double Quotes--
--Replace Double Quotes With Single Quotes--
--Remove Backslashes At End Of Lines--
--You May Also Need To Place All In A Single Line--
do shell script "curl -X POST https://content.dropboxapi.com/2/files/upload --header 'Authorization: Bearer MYTOKEN' --header 'Content-Type: application/octet-stream' --header 'Dropbox-API-Arg: {'path':'/RoomVT/Clown2.jpeg'}' --data-binary #'/Splash.jpeg'"
I still get the error.
"Error in call to API function \"files/upload\": HTTP header \"Dropbox-API-Arg\": could not decode input as JSON"
Thanks for your comments.
I went with #RobC suggestion and it worked first time.
do shell script "curl -X POST https://content.dropboxapi.com/2/files/upload --header 'Authorization: Bearer b4Itg9wetFIAAAAAAAAD7PPkCmoIE2oXvkx_-nq1L2D5G7Bfla-5LHKHtJqoeBMc' --header 'Content-Type: application/octet-stream' --header 'Dropbox-API-Arg: {\"path\":\"/EngineRoomVT/Clown2.jpeg\"}' --data-binary #\"/Splash.jpeg\""

How to use curl command with data from a variable

I'm trying to make a script that execute a curl command to create a Rabbit Exchange with Rest API.
If I use --data with the definition like the follow:
HTTP_RESPONSE=$(curl --silent --write-out "HTTPSTATUS:%{http_code}" -u $USR:$PWD --header "Content-Type: application/json" --request PUT --data '{"type": "topic","auto_delete": false,"durable": true,"internal": false,"arguments": {}}' $URL_GENERATE_EXCHANGE)
It runs
But I want to pass the data from a variable like this
EXCHANGE_DEFINITION='{"type": "topic","auto_delete": false,"durable": true,"internal": false,"arguments": {}}'
HTTP_RESPONSE=$(curl --silent --write-out "HTTPSTATUS:%{http_code}" -u $USR:$PWD --header "Content-Type: application/json" --request PUT --data $EXCHANGE_DEFINITION $URL_GENERATE_EXCHANGE)
but It doesn't work...and I don't understand why....Can anyone help me please?
Thank you

How to pass --data "{'myuid':'testuser1'}" while testing Rest API with the RestClient

I am testing the following API in RestClient but I am not getting the response because I don't know how to pass the --data "{'myuid':'testuser1'}".
curl -k -v -c cookies.txt -b cookies.txt
-X POST
-H "Content-Type:application/json" -H "Accept: application/json"
--data "{'myuid':'testuser1'}"
"https://<isam address>/mgaapi/sps/apiauthsvc?PolicyId=urn:ibm:security:authentication:asf:mytotp"
I have attached the screenshot as well.
How should I correctly pass the --data "{'myuid':'testuser1'}" in the RestClient to get response from the server?
RestAPI Testing
Try this out with little (proper and required) formatting :
curl -k -v -c cookies.txt -b cookies.txt
-X POST
-H "Content-Type:application/json"
-H "Accept: application/json"
--data '{"myuid":"testuser1"}'
https://<aa>/mgaapi/sps/apiauthsvc?PolicyId=urn:ibm:security:authentication:asf:mytotp
Format is this:
curl -X POST -H "Content-Type: application/json" -d '{"key":"val"}' URL

How to PUT a json object with an array using curl

I have a series of data to enter into database. The user interface to enter the data isn't good for bulk entry, so I'm trying to formulate a command line equivalent. When I examine the network request of the UI in chrome, I see a PUT request of a json object. When I try to replicate the request
curl -H 'Accept: application/json' -X PUT '{"tags":["tag1","tag2"],"question":"Which band?","answers":[{"id":"a0","answer":"Answer1"},{"id":"a1","answer":"answer2"}]}' http://example.com/service`
I get a error
curl: (3) [globbing] nested braces not supported at pos X
Where X is the character position of first "[".
How can I PUT a json object that includes an array?
Your command line should have a -d/--data inserted before the string you want to send in the PUT, and you want to set the Content-Type and not Accept.
curl -H 'Content-Type: application/json' -X PUT -d '[JSON]' \
http://example.com/service
Using the exact JSON data from the question, the full command line would become:
curl -H 'Content-Type: application/json' -X PUT \
-d '{"tags":["tag1","tag2"],
"question":"Which band?",
"answers":[{"id":"a0","answer":"Answer1"},
{"id":"a1","answer":"answer2"}]}' \
http://example.com/service
Note: JSON data wrapped only for readability, not valid for curl request.
Although the original post had other issues (i.e. the missing "-d"), the error message is more generic.
curl: (3) [globbing] nested braces not supported at pos X
This is because curly braces {} and square brackets [] are special globbing characters in curl.
To turn this globbing off, use the "-g" option.
As an example, the following Solr facet query will fail without the "-g" to turn off curl globbing:
curl -g 'http://localhost:8983/solr/query?json.facet={x:{terms:"myfield"}}'
It should be mentioned that the Accept header tells the server something about what we are accepting back, whereas the relevant header in this context is Content-Type
It's often advisable to specify Content-Type as application/json when sending JSON. For curl the syntax is:
-H 'Content-Type: application/json'
So the complete curl command will be:
curl -H 'Content-Type: application/json' -H 'Accept: application/json' -X PUT -d '{"tags":["tag1","tag2"],"question":"Which band?","answers":[{"id":"a0","answer":"Answer1"},{"id":"a1","answer":"answer2"}]}' http://example.com/service`
The only thing that helped is to use a file of JSON instead of json body text. Based on How to send file contents as body entity using cURL
Try using a single quote instead of double quotes along with -g
Following scenario worked for me
curl -g -d '{"collection":[{"NumberOfParcels":1,"Weight":1,"Length":1,"Width":1,"Height":1}]}" -H "Accept: application/json" -H "Content-Type: application/json" --user test#testmail.com:123456 -X POST https://yoururl.com
WITH
curl -g -d "{'collection':[{'NumberOfParcels':1,'Weight':1,'Length':1,'Width':1,'Height':1}]}" -H "Accept: application/json" -H "Content-Type: application/json" --user test#testmail.com:123456 -X POST https://yoururl.com
This especially resolved my error curl command error : bad url colon is first character