Using CURL to upload to Dropbox and overwrite a file - linux

I'm a small time admin and would say entry level to Linux. I am trying to use CURL to upload to Dropbox a small backup sqlitedb and have had success for the first upload, however, I am trying to accomplish uploading a file to Dropbox every 30 minutes and overwriting the current file in DROPBOX with the new file from my Linux server (as a jerry-rigged offsite backup of my Teamspeak database)
This is the code I have so far :
curl -X PUT https://content.dropboxapi.com/2/files/upload \
--header "Authorization: Bearer <dropbox code here>" \
--header "Dropbox-API-Arg: {\"path\": \"/home/ec2-user/ts3server.sqlitedb.bz2\"}" \
--header "Content-Type: application/octet-stream" \
--data-binary ts3server.sqlitedb.bz2
After running that code once, it doesn't OVERRIDE the current file in my Dropbox account with the updated file. Any help is appreciated

you need to use mode with parameter overwrite. The default is add.
Add - Do not overwrite an existing file if there is a conflict. The autorename strategy is to append a number to the file name. For
example, "document.txt" might become "document (2).txt".
overwrite - Always overwrite the existing file. The autorename strategy is the same as it is for add.
Reference: /upload
curl -X POST https://content.dropboxapi.com/2/files/upload \
--header "Authorization: Bearer " \
--header "Dropbox-API-Arg: {\"path\": \"/Homework/math/Matrices.txt\",\"mode\": \"overwrite\",\"autorename\": true,\"mute\": false}" \
--header "Content-Type: application/octet-stream" \
--data-binary #local_file.txt

Related

Store output of command in variable where the command is already executed/stored in it's own variable - bash script

I have a command that is stored in a variable. I would like to execute that command and then store the response of that command in a variable. I'm having an issue with syntax errors for the responseVar=$("{commandVar}") line as it seems to trigger a "no such file or directory" error instead of actually running the command/storing the output in the response variable.
commandVar="curl --location -g --request POST 'https://172.217.25.35/rest-gateway/rest/api/v1/auth/token' --header 'Content-Type: application/json' --header 'Authorization: Basic U0FNcWE6NTYyMFNhbSE=' --data-raw '{"grant_type": "client_credentials"}'"
responseVar=$("{commandVar}")
echo "Response of command: ${responseVar}"
Does anyone know the correct syntax on how I would do this? I need to eventually parse the output of the command which is why I need to store the response in it's own variable.
You are trying to execute a program named {commandVar}, and this program does not exist in your PATH.
To start with, you better use an array to store your command, each array element holding one command argument:
commandVar=(curl --location -g --request POST 'https://172.217.25.35/rest-gateway/rest/api/v1/auth/token' --header 'Content-Type: application/json' --header 'Authorization: Basic U0FNcWE6NTYyMFNhbSE=' --data-raw '{"grant_type": "client_credentials"}')
With this, you can execute it by
"${commandVar[#]}"
respectively store its standardoutput into a variable like this:
responseVar=$( "${commandVar[#]}" )

How to send file content as string in data raw payload using cURL post request

Below is my curl query to send the post request
in the "payload" key of the json data i need to send file that is 1 MB of text data as values. Instead of copy past the whole text as string this should take the text as string from locally saved file in my drive.
curl --location --request POST 'http://localhost:8080/test' \
--header 'Content-Type: application/json' \
--data-raw '{
"fileFormat":"csv",
"version":"v1",
"payload":"<local file content should come here>",
}
Put your data payload into the file and try with this: curl --location --request POST 'http://localhost:8080/test' \ --header 'Content-Type: application/json' \ --d #/path/to/filename.json. Hope this will solve your problem

{"error":"incident is missing"} when using PATCH or PUT to update an incident

When I try to submit a PATCH or PUT request to update an incident, I get:
{"error":"incident is missing"}
Here is my request:
curl --location --request PATCH 'https://api.statuspage.io/v1/pages/xxxx/incidents/lf7****nf2?api_key=xxx' \
--header 'Content-Type: application/json' \
--data-raw '{
"incident": "Requests Failed (500)",
"status": "identified"
}'
I have also tried with Authorization: Oauth approach
I am able to create an incident and am also able to list them, so I am unsure what is going on.
I am following https://developer.statuspage.io/#operation/putPagesPageIdIncidentsIncidentId and https://developer.statuspage.io/#operation/patchPagesPageIdIncidentsIncidentId
What am I missing?
Any advice is much appreciated
i need to use https://doers.statuspage.io/api/v1/
curl https://api.statuspage.io/v1/pages/qfn30z5r6s5h/incidents/21w20wsvz5kv.json \
-H "Authorization: OAuth 2a7b9d4aac30956d537ac76850f4d78de30994703680056cc103862d53cf8074" \
-X DELETE

Testing AWS API Gateway with cURL

I do have a simple AWS API Gateway implementation protected by an AWS_IAM Authorization.
I just want to test from command line via cURL :
curl --location --request GET 'https://<API_ID>.execute-api.eu-west-1.amazonaws.com/stage?type=type&category=category&lc=lc&passprhase=passprhase&product=product'
--header 'Authorization: AWS4-HMAC-SHA256 Credential=<AWS_ACCESS_KEY>/20200127/eu-west-1/execute-api/aws4_request, SignedHeaders=host;x-amz-content-sha256;x-amz-date, Signature=<AWS_SECRET_ACCESS_KEY>' --header 'Content-Type: application/json' \
--data-raw '{"query":"","variables":{}}'
but keep getting the follow error :
Authorization header requires existence of either a 'X-Amz-Date' or a 'Date' header.
Can someone advice what am I doing wrong ?
AWS V4 signature authentication is supported in curl starting from version 7.75, so you should be able to call your AWS resource this way:
curl --location --request GET 'https://$API-ID.execute-api.eu-west-1.amazonaws.com/stage?type=type&category=category&lc=lc&passprhase=passprhase&product=product' \
--header 'Content-Type: application/json' \
--user $ACCESS_KEY:$SECRET_KEY \
--aws-sigv4 "aws:amz" \
--data-raw '{"query":"","variables":{}}'
Note that you may need to add in the --aws-sigv4 value your region and service.
For example: --aws-sigv4 "aws:amz:eu-west-2:execute-api"
You can find more documentation here: https://curl.se/libcurl/c/CURLOPT_AWS_SIGV4.html
And the documentation for the CLI option here: https://curl.se/docs/manpage.html#--aws-sigv4
AWS_IAM authorization uses Sigv4 and its calculation process requires values certain headers - Date being one of them. You are passing x-amz-date as a part of the "SignedHeaders" field, but not actually passing it with the other headers.
One way to create the right curl command to invoke an API with AWS_IAM would be to use Postman application. Add in the API URL and select "AWS Signature" under Authorization tab. You can then select the "Code" option and get the full curl command which would look something like this -
curl -X POST \
https://$API-ID.execute-api.$AWS_REGION.amazonaws.com/$STAGE/$RESOURCE \
-H 'authorization: AWS4-HMAC-SHA256 Credential=$ACCESS_KEY/20200128/$AWS_REGION/execute-api/aws4_request, SignedHeaders=content-type;host;x-amz-date, Signature=$SIGNATURE_VALUE' \
-H 'cache-control: no-cache' \
-H 'content-type: application/x-www-form-urlencoded' \
-H 'host: API-ID.execute-api.$AWS_REGION.amazonaws.com' \
-H 'postman-token: 15f9498e-95b7-f22b-eed9-016cdea07424' \
-H 'x-amz-date: $DATE_STAMP'
Create a Canonical Request for Signature Version 4
I could suggest to use awscurl which is much easier.
To install awscurl click here. For documentation you can refer here.
Example to call apigateway to call lambda for POST query is below.
awscurl --service execute-api -X POST -d '{ "alias" : "xyx", "type" : "LDAP" }' https://.execute-api.us-west-2.amazonaws.com/Prod/user/groups/get --region us-west-2 --access_key ACCESS_KEY --secret_key mfBl0YJRsXDue4C5F5B6rz1eUpQpA8uC24RtSnsg --security_token SECURITY_TOKEN

Want to call batch file with node script, get garbled time

I want to create a cron job with a batch file which is using curl to login into a page saving the login token and then run a node script to do something. But when I call my batch file I get
batch: garbled time
My script:
#!/bin/bash
# A simple script to automate something
curl 'someurl' -H 'content-type: application/json' -H 'Accept: */*' -H 'Cache-Control: no-cache' -H 'Referer: someurl' --data-binary '{"some login data"}' -i -O
node doSomething.js
If you are trying to submit your script as a crontab file, then it's in the wrong format. You need to specify time-related fields at the start of the line. See https://en.wikipedia.org/wiki/Cron
The error message you got is typical of a bad time specification.

Resources