0

I am trying to write output from API request (passing through shell command) to JSON file using Python.

import os
assignments = os.system("curl -u https://apitest.com/api/-u domain:jsdjbfkjsbdfden")

I'm getting a response in string format. How I can save this response to a JSON file?

I tried with the request library in Python with the same domain name and api_key, and I'm not sure why I get a 404 error {"error":"Invalid api id. Verify your subdomain parameter"}

import requests
from requests.auth import HTTPBasicAuth 
url = "https://apitest.com/api/"
headers = {"SUBDOMAIN":"domain","api_key": "jsdjbfkjsbdfden"}
authParams = HTTPBasicAuth('[email protected]', 'password@')
response = requests.get(url,headers=headers,auth = authParams)

3 Answers 3

0

You should be using the requests library instead of system calls.

import requests
r = requests.get('https://postman-echo.com/get?foo1=bar1&foo2=bar2')
print(r.content)

Writing to a file is covered in many tutorials across the internet such as w3schools and has been covered extensively on StackOverflow already.

Sign up to request clarification or add additional context in comments.

3 Comments

I tried with request library and i got the 404 {"error":"Invalid api id. Verify your subdomain parameter"} in the response.
What domain are you trying to pass? Do you need to read more on requests? Update your question if your code/question is changing, please.
curl apitest.com/api -u "SUBDOMAIN:API_KEY" I am giving this command in the shell which is working fine and i am getting the response that I expected , when i tried mimic the same with request library i am getting 404 {"error":"Invalid api id. Verify your subdomain parameter"}
0

If you have to use a command:

import os
assignments = os.system("curl -u https://apitest.com/api/-u domain:jsdjbfkjsbdfden > somefile")

There's no real reason to use python's requests module persé except for purity, however keeping it pure python helps portability.

Comments

0

Is it not easier to use a "requests" library to make queries?

import requests
link = "" #your link

myobj = {'somekey': 'somevalue'}

r = requests.post(link, data=myobj)
r.status_code

Comments

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.