简体   繁体   中英

How to download file(*.txt or *.pdf) which received from GET api response through Curl command?

I want to download files(*.txt and *.pdf) from Ariba site through GET api request and want to automate the whole download process. Initially I have used Postman for testing purpose which gives me result in the form of file content. For eg. test.txt file is present on remote site, after GET request from Postman, the result I am getting, it is in form of content of file, so if 'abc' is written in file, I am getting 'abc' as response from test.txt file.

Now if I click on Send and Download button in Postman it gives me option to download file 'test.txt'

I have to automate this process to send GET response and get required file download at specific location. I am trying to use Curl script for this.

I have written corresponding Curl script and tried to execute it. It gives me response in the form of file content.

curl -X GET \
  'https://openapi.ariba.com/api/approval/v1/prod/invoices/INVASINV6-902/attachments/bnMyMDE5LzA0LzAzLzE1MjkyNDE4MQ==?realm=ProjectName&Content-Disposition=attachement' \
  -H 'Accept: */*' \
  -H 'Authorization: Bearer 7648d29a-db04-4046-b49c-5daed43a145c' \
  -H 'Cache-Control: no-cache' \
  -H 'Connection: keep-alive' \
  -H 'Host: openapi.ariba.com' \
  -H 'accept-encoding: gzip, deflate' \
  -H 'apiKey: xxxxxxxxxxxxxxxxxx' \
  -H 'cache-control: no-cache'

I want to write a curl script which will download file at specific location. For eg. Above curl command give content(abc) from file Test.txt and not file Test.txt as output

Really appreciate your help Curl Get Response

Postman Get Response

Finally I got answer to the question, I just need to remove -X GET from my curl script and at the end add -o to download file name. Below is the final code:

curl  'https://openapi.ariba.com/api/approval/v1/prod/invoices/INVASINV6-902/attachments/bnMyMDE5LxxxxLzE1MjkyNDE4MQ==?realm=ProjectName' \
  -H 'Accept: */*' \
  -H 'Authorization: Bearer 77876887-xxxxx-42fb-b865-9cf8ff5c2b25' \
  -H 'Cache-Control: no-cache' \
  -H 'Connection: keep-alive' \
  -H 'Host: openapi.ariba.com' \
  -H 'accept-encoding: gzip, deflate' \
  -H 'apiKey: XXXXXXXXXXXXXXXX' \
  -H 'cache-control: no-cache' \
  -H 'Content-Type: application/octet-stream' \
  -H "Content-Transfer-Encoding: Binary" \
  -o "Test.txt"

Above code give me file downloaded at specific location. Thanks BlackPearl.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM