1

Currently, I'm trying to convert CURL request to Python script.

curl $(curl -u username:password -s https://api.example.com/v1.1/reports/11111?fields=download | jq ".report.download" -r) > "C:\sample.zip"

I have tried pycurl, with no success, due to knowledge limitation.

As a solution, I have found, that it is possible to run commands through python. https://www.raspberrypi.org/forums/viewtopic.php?t=112351

import os
os.system("curl -K.........")

And other solution ( based on the search more common) using subprocess

import subprocess

subprocess.call(['command', 'argument'])

Currently, I'm not sure where to move and how to adapt this solution to my sitionation.

import os

os.system("curl $(curl -u username:password -s https://api.example.com/v1.1/reports/11111?fields=download | jq '.report.download' -r) > 'C:\sample.zip'")

'curl' is not recognized as an internal or external command,
operable program or batch file.
255

P.S. - Update v1

Any suggestion?

import requests

response = requests.get('https://api.example.com/v1.1/reports/11111?fields=download | jq ".report.download" -r', auth=('username', 'password'))

This work without "| jq ".report.download" this part, but this is the main part which gives at the end only link to download the file.

ANy way around it?

5
  • 2
    requests module should help you Commented Jun 15, 2018 at 8:09
  • curl executable is not in PATH environment variable, Try to add curl to path or install it from apt using apt-get install curl Commented Jun 15, 2018 at 8:12
  • @Rakesh have updated ( please have a look ) Commented Jun 15, 2018 at 8:29
  • @sriharsha_bhat that have potential, but issue appear when I try to use JQ. Commented Jun 15, 2018 at 8:30
  • @Rakesh Look, the request does work, BUT jq section after the pipe do not work which gives me instead of full JSON just the content of it, any suggestions??? Commented Jun 15, 2018 at 8:44

2 Answers 2

2

The error 'curl' is not recognized as an internal or external command means that python couldn't find the location where curl is installed. If you have already installed curl, try giving the full path to where curl is installed. For example, if curl.exe is located in C:\System32, the try

import os

os.system("C:\System32\curl $(curl -u username:password -s https://api.example.com/v1.1/reports/11111?fields=download | jq '.report.download' -r) > 'C:\sample.zip'")

But thats definitely not pythonic way of doing things. I would instead suggest to use requests module. You need to invoke requests module twice for this, first to download the json content from https://api.example.com/v1.1/reports/11111?fields=download, get a new url pointed byreport.download and then invoke requests again to download data from the new url.

Something along these lines should get you going

import requests

url        = 'https://api.example.com/v1.1/reports/11111'
response   = requests.get(url, params=(('fields', 'download'),),
                          auth=('username', 'password'))

report_url = response.json()['report']['download']
data = requests.get(report_url).content

with open('C:\sample.zip', 'w') as f:
    f.write(data)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for this help, I'm really appreciated.
1

You can use this site to convert the actual curl part of your command to something that works with requests: https://curl.trillworks.com/

From there, just use the .json() method of the request object to do whatever processing you need to be doing.

Finally, can save like so:

import json
with open('C:\sample.zip', 'r') as f:
    json.dump(data, f)

1 Comment

This is the website which I have used for other parts to convert, but this specific part, I could not manage to convert yet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.