Post file from Bash shell script - bash

I need to post a PowerPoint file to an online service which converts PPT files to PDF. I am new to Bash shell and don't know how to post PPT file from my local computer. How should I modify line below?
curl -i -H "Accept: application/pdf" -X POST http://do.convertapi.com/PowerPoint2Pdf > output

Use the --data-binary <data> option.
$ curl --data-binary #your.pdf ...

Related

Hiding data/ password while trying curl - bash [duplicate]

I need to make a POST request via cURL from the command line. Data for this request is located in a file. I know that via PUT this could be done with the --upload-file option.
curl host:port/post-file -H "Content-Type: text/xml" --data "contents_of_file"
You're looking for the --data-binary argument:
curl -i -X POST host:port/post-file \
-H "Content-Type: text/xml" \
--data-binary "#path/to/file"
In the example above, -i prints out all the headers so that you can see what's going on, and -X POST makes it explicit that this is a post. Both of these can be safely omitted without changing the behaviour on the wire. The path to the file needs to be preceded by an # symbol, so curl knows to read from a file.
I need to make a POST request via Curl from the command line. Data for this request is located in a file...
All you need to do is have the --data argument start with a #:
curl -H "Content-Type: text/xml" --data "#path_of_file" host:port/post-file-path
For example, if you have the data in a file called stuff.xml then you would do something like:
curl -H "Content-Type: text/xml" --data "#stuff.xml" host:port/post-file-path
The stuff.xml filename can be replaced with a relative or full path to the file: #../xml/stuff.xml, #/var/tmp/stuff.xml, ...
If you are using form data to upload file,in which a parameter name must be specified , you can use:
curl -X POST -i -F "parametername=#filename" -F "additional_parm=param2" host:port/xxx
Most of answers are perfect here, but when I landed here for my particular problem, I have to upload binary file (XLSX spread sheet) using POST method, I see one thing missing, i.e. usually its not just file you load, you may have more form data elements, like comment to file or tags to file etc as was my case. Hence, I would like to add it here as it was my use case, so that it could help others.
curl -POST -F comment=mycomment -F file_type=XLSX -F file_data=#/your/path/to/file.XLSX http://yourhost.example.com/api/example_url
I was having a similar issue in passing the file as a param. Using -F allowed the file to be passed as form data, but the content type of the file was application/octet-stream. My endpoint was expecting text/csv.
You are able to set the MIME type of the file with the following syntax:
-F 'file=#path/to/file;type=<MIME_TYPE>
So the full cURL command would look like this for a CSV file:
curl -X POST -F 'file=#path/to/file.csv;type=text/csv' https://test.com
There is good documentation on this and other options here: https://catonmat.net/cookbooks/curl/make-post-request#post-form-data
I had to use a HTTP connection, because on HTTPS there is default file size limit.
https://techcommunity.microsoft.com/t5/IIS-Support-Blog/Solution-for-Request-Entity-Too-Large-error/ba-p/501134
curl -i -X 'POST' -F 'file=#/home/testeincremental.xlsx' 'http://example.com/upload.aspx?user=example&password=example123&type=XLSX'

Api curl script is not showing output in my unix terminal

I wrote a curl script to pull data from my api url into my unix terminal. But when I run the script below, I get a blank space as my output, rather than data from the api url. I don't get any standard error message, just a blank space followed by $ to enter a new command.
#!/bin/bash
INSTANCE_NAME="https://servicenow.com/ServiceNowData/tickets?ticks=4320000"
DATA_OUTPUT=$(curl -s -k -X GET -H "accept: application/json" $INSTANCE_NAME)
echo $DATA_OUTPUT
Page 404. And -H "Accept-Encoding: application/json"

passing execute shell data between build steps in jenkins

Have 2 different execute shells in jenkins and need to pass a variable from one to another
Code below shows what i have tried
Shell script #1
export storedBanner=$(curl http://my.network:8080/boardmessage | jq -r .[0].message|sed 's/<[^>]*>//g')
echo $storedBanner > ~/stored.txt
curl -i -X POST -H "Content-Type: application/json" -d "{\"message\":\"<h3>Test message<h3>\"}" http://my.network:8080/boardmessage
Shell script #2
export storedBanner= $(cat ~/stored.txt)
curl -X POST -H "Content-Type: application/json" -d "{\"message\":\"<h4>${storedBanner}<h4>\"}" http://my.network:8080/boardmessage
I want that the exported message to be stored and passed down to 2nd shell script however this doesnt seem to work.
you can use a global variable to do that
Thanks for help, I figured it out with the file and it turns out i used the wrong syntax
Putting this in the shell script #2 worked :)
storedBanner=$(<banner.txt)

POST multiple files with -d in curl

I'm using curl to create several classifications. I have written the json for the many classifications and they are in one folder. I would like to create all the classifications in one go. But using curl I can only create them one at a time. How could I make them in one request?
curl -u admin:admin -H "Content-Type: application/json" -X POST -d #pii.json http://127.0.0.1:21000/api/atlas/v2/types/typedefs
The curl manual for -d says 'Multiple files can also be specified'. How can I do this? All my attempts have failed.
Do I need a bash script instead? If so, could you help me - I'm not a coder and I'm struggling without an example!
Thanks in advance.
You probably don't want to use multiple -d with JSON data since curl concatenates multiple ones with a & in between. As described in the man page for -d/--data:
If any of these options is used more than once on the same command
line, the data pieces specified will be merged together with a
separating &-symbol. Thus, using '-d name=daniel -d skill=lousy' would
generate a post chunk that looks like 'name=daniel&skill=lousy'.
You can however easily and conveniently pass several files on stdin to let curl use them all in one go:
cat a.json b.json c.json | curl -d#- -u admin:admin -H "Content-Type: application/json" http://127.0.0.1:21000/api/atlas/v2/types/typedefs
(please note that -X POST has no place on a command line that uses -d)
I found the following to work in the end:
<fileToUpload.dat xargs -I % curl -X POST -T "{%}" -u admin:admin -H "Content-Type: application/json" http://127.0.0.1:21000/api/atlas/v2/types/typedefs
Where fileToUpload.dat contained a list of the .json files.
This seemed to work over Daniel's answer, probably due to the contents of the files. Hopefully this is useful to others if Daniel's solution doesn't work for them.
I needed to upload all the *.json files from a folder via curl and I made this little script.
nfiles=*.json
echo "Enter user:"
read user
echo "Enter password:"
read -s password
for file in $nfiles
do
echo -e "\n----$file----"
curl --user $user:$password -i -X POST "https://foo.bar/foo/bar" -H "Content-Type: application/json" -d "#$file"
done
Maybe fits your needs.

How to change the status of WorkItem in RTC

I need to chage the status of a workitem(Task) in RTC throgh VBScript.
I've tried like this:
rtc_cm:status,https:/local/ccm/resource/itemName/com.ibm.team.workitem.defectWorkflow.action.startWorking
This is not working. Someone please help?
Since there is no command line for Work Items, you need to use the REST API:
URL="https://localhost:9443/jazz/resource/itemName/com.ibm.team.workitem.WorkItem/821"
curl -D - -k -b $COOKIES -o "wi-821.json" -H "Accept: application/x-oslc-cm-changerequest+json" $URL
=> Modify what you need in that wi-821.json file, like the rtc_cm:state, and post it back
URL="https://localhost:9443/jazz/resource/itemName/com.ibm.team.workitem.WorkItem/821"
curl -D - -k -b $COOKIES -H "If-Match: _1am9cFm0Ed6ELJg2MQ68Kg" -H "Content-Type: application/x-oslc-cm-change-request+json" -H "Accept: application/x-oslc-cm-change-request+json" -X PUT --data-binary #wi-821.json $URL
This isn't in VB, but you can adapt it in order to encapsulate it in VB.
According to https://jazz.net/wiki/bin/view/Main/ResourceOrientedWorkItemAPIv2#Attributes the RTC status is "modifiable via action". You need to use a URL like:
set WORK_ITEM_ID=123456
set URL="https://localhost:9443/ccm/oslc/workitems/%WORK_ITEM_ID%?_action=com.ibm.team.workitem.activityWorkflow.action.startWorking"
curl -D - -k -b %COOKIES% -H "Content-Type: application/x-oslc-cm-change-request+xml" -H "Accept: application/x-oslc-cm-change-request+xml" -X PUT --data-binary #input.xml %URL%
Where input.xml contains the following:
<?xml version="1.0" encoding="UTF-8"?>
<oslc_cm:ChangeRequest xmlns:oslc_cm="http://open-services.net/xmlns/cm/1.0/"
xmlns:calm="http://jazz.net/xmlns/prod/jazz/calm/1.0/"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:dc="http://purl.org/dc/terms/"
xmlns:oslc_pl="http://open-services.net/ns/pl#"
xmlns:rtc_cm="http://jazz.net/xmlns/prod/jazz/rtc/cm/1.0/"
xmlns:oslc_cmx="http://open-services.net/ns/cm-x#">
</oslc_cm:ChangeRequest>
You can get the list of supported actions in Eclipse by going Work Items > Team Artifacts > Right-click on Project Area > Open > Process Configuration Source tab. Search the displayed text for e.g. "com.ibm.team.workitem.activityWorkflow" to see Activity actions.
I'm using XML here but the same procedure should apply when using JSON.

Resources