Submitting via curl in a bash script to sharepoint - json

I've been scratching my head for a couple of days on this one. Google-foo, sadly, isn't turning up much. I'd like to stay in bash if at all possible for portability
This works:
node="$(curl -s --anyauth -u dom\\r-hah:haha -d '{"Filename":"0117-SCLP1-6230-0_E_H.mp4",
"MediaPath":"\/2016",
"SpotNumber":"0117-SCLP1-6230-0 E H",
"SpotName":"TEST FOR TECH",
"Description":"TEST SPOT ONLY",
"ShipListPromoID":"467733"}' -H content-type:application/json -X POST https://somewebsite.com/fcp/_vti_bin/ListData.svc/FinishedSpots);
echo "${node}"
This does not:
dslresults="$(php /usr/local/sbin/lookupupfrontspot.php "0117-SCLP1-6230-0_E_H.mp4")";
echo "${dslresults}";
node="$(curl -s --anyauth -u dom\\r-hah:haha -d '$dslresults' -H content-type:application/json -X POST https://somewebsite.com/_vti_bin/ListData.svc/FinishedSpots);
echo "${node}"
Let me add that in the first example, I am copy and pasting the results of echo "${dslresults}" from the second example. So I'm guessing it's some kind of formatting error, but I can't seem to identify it.
Any obvious snafus I'm doing? (I'm sure there's tons)
Thanks much.
-N

Related

How to POST two files

I am trying to post two different files using the command
curl -vX POST http://localhost/api/postJSON -d #input.json -d #input2.json --header "Content-Type: application/json"
I know something is wrong. Does anyone have any idea about this?
Consulting the curl manpage and identify the -F option (https://curl.haxx.se/docs/manpage.html#-F).
There's also an example of uploading multiple files via curl at https://medium.com/#petehouston/upload-files-with-curl-93064dcccc76#e822
$ curl -F 'fileX=#/path/to/fileX' -F 'fileY=#/path/to/fileY' ... http://localhost/api/postJSON upload

CURL command not executing in SSH

I am trying to execute a CURL command to set a greeting message for a bot I am building. Guidance from Facebook here: https://developers.facebook.com/docs/messenger-platform/thread-settings/greeting-text
Example from Facebook:
curl -X POST -H "Content-Type: application/json" -d '{
"setting_type":"greeting",
"greeting":{
"text":"Timeless apparel for the masses."
}
}' "https://graph.facebook.com/v2.6/me/thread_settings?access_token=PAGE_ACCESS_TOKEN"
I am trying to run that from a SSH command prompt -- the only change I made was make it into 1 line, so that I can execute it from command line in one go.
curl -X POST -H "Content-Type: application/json" -d '{"setting_type":"greeting", "greeting":{"text":"Hi {{user_first_name}}! I am a bot. Simply say 'Hi' to start.. yes, it's as simple as that!"}}' "https://graph.facebook.com/v2.6/me/thread_settings?access_token=EAAFXmrm6M8cBAHF2TwfWMnQfksHnZBtQQIHYWjIFpmTffkG"
But, I get this error message from SSH when trying to execute above 1 liner.
-bash: !"}}': event not found
What am I doing wrong here? Any help is appreciated.
You have a problem with single quotes ', you can replace them with \u0027 :
curl -X POST -H "Content-Type: application/json" -d '{"setting_type":"greeting", "greeting":{"text":"Hi {{user_first_name}}! I am a bot. Simply say \u0027Hi\u0027 to start.. yes, it\u0027s as simple as that!"}}' "https://graph.facebook.com/v2.6/me/thread_settings?access_token=EAAFXmrm6M8cBAHF2TwfWMnQfksHnZBtQQIHYWjIFpmTffkG"

How to echo variable inside single quotes using Bash?

users.I want to run a curl command with a bash script.
(below command works perfectly in terminal)
curl -i -H "Content-Type: application/json" -X POST -d '{"mountpoint":"/gua-la-autentica-1426251559"}' http://127.0.0.1:5000/connect
But unable to run this command in bash. When mountpoint value is given in a variable($final).
final="/gua-la-autentica-1426251559"
curl -i -H "Content-Type: application/json" -X POST -d '{"mountpoint":'$final'}' http://127.0.0.1:5000/connect
Could someone please help me, how to echo variable inside single quotes?
JSON string values should be quoted and so should parameter expansions. You can achieve this by using double quotes around the entire JSON string and escaping the inner double quotes, like this:
curl -i -H "Content-Type: application/json" -X POST -d "{\"mountpoint\":\"$final\"}" http://127.0.0.1:5000/connect
As mentioned in the comments, a more robust approach would be to use a tool such as jq to generate the JSON:
json=$(jq -n --arg final "$final" '{ mountpoint: $final }')
curl -i -H "Content-Type: application/json" -X POST -d "$json" http://127.0.0.1:5000/connect
I'd probably do it with a printf format. It makes it easier to see formatting errors, and gives you better control over your output:
final="/gua-la-autentica-1426251559"
fmt='{"mountpoint":"%s"}'
curl -i -H "Content-Type: application/json" -X POST \
-d "$(printf "$fmt" "$final")" \
http://127.0.0.1:5000/connect
I don't know where you're getting $final in your actual use case, but you might also want to consider checking it for content that would break your JSON. For example:
Portable version:
if expr "$final" : '[A-Za-z0-9./]*$'; then
curl ...
else
echo "ERROR"
fi
Bash-only version (perhaps better performance but less portable):
if [[ "$final" ]~ ^[A-Za-z0-9./]*$ ]]; then
curl ...
...
Checking your input is important if there's even the remote possibility that your $final variable will be something other than what you're expecting. You don't have valid JSON anymore if it somehow includes a double quote.
Salt to taste.

How Can I Post Files and JSON Data Together With Curl?

I've been posting a file with this curl command:
curl -i -F file=#./File.xlsm -F name=file -X POST http://example.com/new_file/
Now I want to send some information about the file (as JSON) along with the file.
curl -i -H "Content-Type: application/json" -d '{"metadata": {"comment": "Submitting a new data set.", "current": false }, "sheet": 1, "row": 7 }' -F file=#./File.xlsm -F name=file http://example.com/new_file/
Curl is very grumpy about being used in this completely incorrect way, and in this case it says "You can only select one HTTP request!" OK, fair enough, so how do I get the file attachment and those POST variables into a single curl HTTP request?
I've had success developing similar endpoints that accept multiple files along with their metadata in JSON format.
curl -i -X POST -H "Content-Type: multipart/mixed" -F "blob=#/Users/username/Documents/bio.jpg" -F "metadata={\"edipi\":123456789,\"firstName\":\"John\",\"lastName\":\"Smith\",\"email\":\"john.smith#gmail.com\"};type=application/json" http://localhost:8080/api/v1/user/
Notice the addition of ;type=application/json at the end of the metadata request part. When uploading multiple files of different types, you can define the mime type at the end of the -F value.
I have confirmed that this works for Spring MVC 4.3.7 using #RequestPart. The key in that instance is to not provide the consumes value on the #RequestMapping annotation.
You could just add another form field:
curl -X POST http://someurl/someresource -F upload=#/path/to/some/file -F data="{\"test\":\"test\"}"
Note: due to the content type, this does not really equate to sending json to the web service.
This worked for me:
curl -v -H "Content-Type:multipart/form-data" -F "meta-data=#C:\Users\saurabh.sharma\Desktop\test.json;type=application/json" -F "file-data=#C:\Users\saurabh.sharma\Pictures\Saved Pictures\windows_70-wallpaper.jpg" http://localhost:7002/test/upload
test.json has the json data I want to send.
From #nbrooks comment, adding an additional header HTTP header works fine as shown below by using several -H or --header flags to your curl command:
curl -H "comment: Submitting a new data set." -H "current: false" -H "sheet: 1" -H "row: 7" -F file=#./File.xlsm -F name=file http://example.com/new_file/
comment and current can be combined into "metadata" in the
request.headers processing part on the flask web server.

execute curl command in ruby code

i am trying to run following curl command from my ruby code
Kernel.system'curl -H "Content-Type:application/json" -H "Accept:application/json" -d "{\"project\":{\"name\":\"BB\",\"description\":\"Book\"}}" "http://localhost:3000/company/projects?auth_token=mRFWyxfdPKHsDb4HhyLP"'
I want to do something like
name = "BB"(using a variable)
and then executing
Kernel.system'curl -H "Content-Type:application/json" -H "Accept:application/json" -d "{\"project\":{\"name\":"#{name}",\"description\":\"Book\"}}" "http://localhost:3000/company/projects?auth_token=mRFWyxfdPKHsDb4HhyLP"'
but this approach is not working...its taking #{name} as string.
Please help me with this.Let me know if i am doing something wrong.
Thanks.
in order to enable string interpolation you will need to use double quotes in your string. The topic has been covered here: Double vs single quotes
also it would make your code better to read if you refactored it and assigned the parts to variables before joining them together into one string.
so:
host_name = 'stackoverflow.com'
Kernel.system 'curl -I #{host_name}'
will not work
whereas
Kernel.system "curl -I #{host_name}"
will