Help creating a bash script for a REST API
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

Hi,
I need the following:

I have to run the following command:

curl 'https://mysite.com/api/v1/users/self/files?as_user_id=thisuser:user01' \
-F 'url=https://link.to/my.pdf' \
-F 'name=file_submission1.pdf' \
-F 'parent_folder_path=my_files/section1' \
-H "Authorization: Bearer MYGENERATEDTOKEN"

Which returns:

{"id":40,"upload_status":"pending","status_url":"https://mysite.com/api/v1/files/40/JI57Ivg8LvWEG2bJBB9z8CvDTbYT2j6DJAd4wewL/status"}

I then need to grab the ID, in this case "40" and run it through the next CURL command.

curl 'https://mysite.com/api/v1/courses/2/assignments/1/submissions?isuser:user01' \
-F 'submission[submission_type]=online_upload' \
-F 'submission[file_ids][]=40' \
-H "Authorization: Bearer MYGENERATEDTOKEN"

I then need to run this for users 01-50, in effect so each user uploads a file, and then submits it via the API. I'd like this in the form of a bash script that I can modify on a per use basis, and executable from terminal on a Mac

Cheers!

you will run it with php or directly with bash?
Houcem B. A. Chlegou 27 days ago
awarded to 5osxcwbf

Crowdsource coding tasks.

2 Solutions


#!/bin/bash

i_max=50

for i in {`seq -w 1 $i_max`}
do

json=$(`curl 'https://mysite.com/api/v1/users/self/files?as_user_id=thisuser:user$i' -F 'url=https://link.to/my.pdf' -F 'name=file_submission1.pdf' -F 'parent_folder_path=my_files/section1' -H "Authorization: Bearer MYGENERATEDTOKEN`)

id=$(`$json | grep -Eo '"id":.*?[^\\]",' `)
curl 'https://mysite.com/api/v1/courses/2/assignments/1/submissions?isuser:user$i' -F 'submission[submission_type]=online_upload' -F 'submission[file_ids][]=$id' -H "Authorization: Bearer MYGENERATEDTOKEN"

done
try this and let me know :)
Subhash Dasyam 27 days ago
it seems like he also wants to be able to run it on a per-user basis, also using grep to parse stuff like html, json etc is a bad habit that sooner or later always comes back to haunt you.
5osxcwbf 27 days ago
I understand, but Simon said this need to be run on MAC OsX terminal where jq,python may not be available so i had to choose RE
Subhash Dasyam 27 days ago
Python comes preinstalled on Mac osx.
5osxcwbf 27 days ago
weird not int he case of mine. i had to install
Subhash Dasyam 27 days ago
Winning solution

So here is my solution, if you are unable to modify it yourself, just accept my answer so i get the bounty, and send the urls, access credential detail etc to my anti-spam email address; alt.r2-5osxcwbf@yopmail.com; i will send you the completely working version as soon as i receive your email. Oh and don't forget to run in bash "chmod +x script.sh" since the script executes itself recursively.

#!/bin/bash
args=("$@")
USER=${args[0]}


#if no argument is found, then we must recursively call ourselves; ./script.sh user01, ./script.sh user02 etc

if [ -z  "$USER" ]; then
    for i in `seq -w 01 50`; 
    do
        echo "Recursively execute $0 user$i"
        #comment out below line to prevent everything from really executing
        $0 user$i

        #remove bellow comment to stop sleeping in between requests
        sleep 1
    done  
    exit 1;    
fi 



echo "Getting json for:$USER" 

#hardcoded json, uncomment the real curl request below to overwrite 
JSON='{"id":40,"upload_status":"pending","status_url":"https://mysite.com/api/v1/files/40/JI57Ivg8LvWEG2bJBB9z8CvDTbYT2j6DJAd4wewL/status"}'

#JSON=$( 
#curl "https://mysite.com/api/v1/users/self/files?as_user_id=thisuser:$USER" \
#-F 'url=https://link.to/my.pdf' \
#-F 'name=file_submission1.pdf' \
#-F 'parent_folder_path=my_files/section1' \
#-H "Authorization: Bearer MYGENERATEDTOKEN"  \
#-s
#) 
echo $JSON 

#Filter id from json using python (which comes pre-installled on osx)
ID=$(echo $JSON  | python -c 'import json,sys;obj=json.load(sys.stdin);print obj["id"]')

echo "id: $ID"

#SUBMISSION=$( 
#curl "https://mysite.com/api/v1/courses/2/assignments/1/submissions?isuser:$USER" \
#-F 'submission[submission_type]=online_upload' \
#-F "submission[file_ids][]=$ID" \
#-H "Authorization: Bearer MYGENERATEDTOKEN"  \
#-s
#) 

echo $SUBMISSION
Comments won't parse properly - I'll email you what I get
Simon 27 days ago
Just for clarity - the ID changes with ever user, so I've uncommented out the first CURL command - however it looks like the python command is not working: File "", line 1, in KeyError: 'id' id:
Simon 27 days ago
I just emailed you :]
5osxcwbf 27 days ago
Hello 5os, I need a brilliant developer to solve several ffmpeg issues. I can increase the budget. I have just posted a bounty of $10 as I need to know if anyone is capable of solving ffmpeg tasks. You have solved complex bounties before. Do you think you can take this one and more related ffmpeg bounties? Please see https://bountify.co/ffmpeg-add-audio-fx-on-transition . Thank you!
georgefountain 8 days ago
View Timeline