I am trying to post the following JSON to a URL using cURL in Terminal:
[
{
"token": "ABCDEF",
"templateId": "{1234-5678-9}",
"senders": "null",
"viewers": "null",
"peoples": "null",
"fields": {
"Matter Name": "My test matter name",
"Matter Number": "ABC123"
}
}
]
This is how I POST it in Terminal:
curl -v -k -X POST -H "Content-Type: application/json" -d docfile=#test.json https://myWebsite.com/extension/extension/extension
The Terminal output clearly says that something was posted but the part that confuses me is this excerpt from the output: upload completely sent off: 18 out of 18 bytes
Only 18 bytes were sent? My file is 218 bytes...Why is this file not being POSTed? What is being POSTed?
The problem is with your -d switch. From the documentation:
-d
Sends the specified data in a POST request to the HTTP server... If you start the data with the letter #, the rest should be a file name to read the data from.
What you are passing to the -d switch does not begin with "#", so it is being interpreted as actual data. You'll notice docfile=#test.json IS actually 18 bytes.
You need to change it from -d docfile=#test.json to -d #test.json.
Related
I have a Nexus Repository server where my artifacts are stored. I want to write a shell script to download artifacts from here. When using the curl request curl --user username:password -X GET "http://your_ip:your_port/service/rest/v1/search?repository=your_repository" -H "accept: application/json" I get a list of the items in my repository which looks like this:
{
"items": [
{
"id": "dGVzdC1hcHA6ZDM1MTBiN2FkMThkODJjZGU1NjNhMWVlMWFmOWIwMGQ",
"repository": "test-app",
"format": "maven2",
"group": "no.ahj",
"name": "test-app",
"version": "1.0-20190715.130341-2",
"assets": [
{
"downloadUrl": "http://192.168.56.2:8081/repository/test-app/no/ahj/test-app/1.0-SNAPSHOT/test-app-1.0-20190715.130341-2.pom",
"path": "no/ahj/test-app/1.0-SNAPSHOT/test-app-1.0-20190715.130341-2.pom",
"id": "dGVzdC1hcHA6Yzc3MDE2OWMwYjJlM2VkODU0MGMyOGEwOWQ0Njk4ZTQ",
"repository": "test-app",
"format": "maven2",
"checksum": {
"sha1": "5fd032774dd3ae6fbbd6484b3dc6ef2582d9b397",
"md5": "3a6aa8e295a734fdb8a8df782c0a14d5"
}
},
I would like my shell script to run this curl request, extract the value from the downloadURL field, store it in some variable and then use wget with this variable to download the file. So my question is this: How can I take the URL from downloadURL and store/use it in my shell script?
A way to parse it with likely no external dependencies (as python is installed by default on most Linux distributions) is just to use python:
user#host ~ % JSON=$(curl --user username:password -X GET "http://your_ip:your_port/service/rest/v1/search?repository=your_repository" -H "accept: application/json")
user#host ~ % echo $JSON | python -c 'import sys, json; print(json.load(sys.stdin)["items"][0]["assets"][0]["downloadUrl"])'
http://192.168.56.2:8081/repository/test-app/no/ahj/test-app/1.0-SNAPSHOT/test-app-1.0-20190715.130341-2.pom
If you are going to do a lot of JSON parsing in this script, it may be worth considering writing the entire script in Python, too, instead of shell script.
This question already has answers here:
Curl command issue in bash script because of string variable containing quotes [duplicate]
(2 answers)
Closed 3 years ago.
I am trying to insert a variable into my json payload (working in shell script) but I am unsure of how to escape the characters properly
I've tried many different escape methods but I'm a total noob at it and I either return the literal string or it doesn't run
SLACK_ALERT_WEBHOOK=desiredurl
curl -X POST -H 'Content-type: application/json' --data '{"text": "*Daily Webhook Verification*", "attachments": [
{
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Slack post failed for webhook, please investigate: $SLACK_ALERT_WEBHOOK"
}
}
]
}
]}' "$SLACK_ALERT_WEBHOOK"
I just want to insert the value of SLACK_ALERT_WEBHOOK into this portion of the code "text": "Slack post failed for webhook, please investigate: $SLACK_ALERT_WEBHOOK but it either doesn't run or returns the literal string. I have the bottom "$SLACK_ALERT_WEBHOOK" working successfully at the bottom to send to my desired slack channel so I'm not worried about that.
I've got it working thanks to tripleee:
curl -X POST -H 'Content-type: application/json' --data "{\"text\": \"*Verification*\", \"attachments\": [{\"blocks\": [{\"type\": \"section\",\"text\": {\"type\": \"mrkdwn\",\"text\": \"$SLACK_ALERT_WEBHOOK\"}}]}]}" $SLACK_ALERT_WEBHOOK
Try this:
SLACK_ALERT_WEBHOOK=desiredurl
$(curl -X POST -H 'Content-type: application/json' --data '{"text": "*Daily Webhook Verification*", "attachments": [
{
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Slack post failed for webhook, please investigate: `echo $SLACK_ALERT_WEBHOOK`"
}
}
]
}
]}' "`echo $SLACK_ALERT_WEBHOOK`")
Using the echo <<variable>> it should just run that as a command. I tested it on my own server, but might work differently on yours. Hope it helps.
In the Watson API documentation, the following is given for adding query to training data using CURL:
curl -X POST -u "{username}":"{password}" -H "Content-Type: application/json" -d
'{
"natural_language_query": "who is keyser soze",
"filter": "text:criminology",
"examples": [
{
"document_id": "adaf50f1-2526-4fad-b670-7d6e8a42e6e6",
"relevance": 2
},
{
"document_id": "63919442-7d5b-4cae-ab7e-56f58b1390fe",
"cross_reference": "my_id_field:14",
"relevance": 4
}
]
}' "https://gateway.watsonplatform.net/discovery/api/v1/environments/{environment_id}/collections/{collection_id}/training_data?version=2018-03-05"
However much I try or use multiple libraries, it does not seem to have the correct JSON format (even when JSON validators have confirmed).
Has anyone tried using this method to add queries?
Im trying to index a simple json data in solr using curl. When i use the command, it says
"curl -X POST -H 'Content-Type:application/json'-d http://localhost:8983/solr/informationretrieval/update/json/docs '[{"id":"1","title":"Doc 1"},{"id":"2","title":"Doc 2"}]'
{"responseHeader":{"status":0,"QTime":1}}
curl: (3) [globbing] bad range specification in column 3"
I have tried removing quotes, tried -g and --globoff etc but every time there are errors like illegal port number. Can anyone please help?
Adding Multiple JSON Documents
curl -X POST -H 'Content-Type: application/json' 'http://localhost:8983/solr/informationretrieval/update' --data-binary '
[
{
"id": "1",
"title": "Doc 1"
},
{
"id": "2",
"title": "Doc 2"
}
]'
I'm getting this error:
error": { "code": 400, "message": "Invalid value at 'requests[0].image.content' (TYPE_BYTES), \"000002.jpg\"", "status": "INVALID_ARGUMENT", "details": [ { "#type": "type.googleapis.com/google.rpc.BadRequest"
And it may have something to do with the encoding of the images, not sure. I am sending Google a bunch of .jpgs.
Note that this is similar to this question, but that one doesn't help a whole lot - there's no answer.
Here's my JSON:
for f in $FILES
do
echo "Original file name is $f"
response=$(curl -v -H "Accept: application/json" -H "Content-type:
application/json" -X POST -d '{"requests":[
{
"image":{
"content":"'"$f"'"
},
"features":[
{
"type":"LABEL_DETECTION",
"maxResults":3
}
]
}]}' $baseURL)
echo $response
done
This is the first image I send it.
Any thoughts on what's causing this?
You need to send the base64 encoded representation of the image, not the filename.