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")

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 same domain name and api_key not sure why i am getting 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('username@gmail.com', 'password@')
response = requests.get(url,headers=headers,auth = authParams)

Any help would be appreciated.

Rahul
  • 371
  • 1
  • 12

3 Answers3

0

is It not easier to use a "requests" libarary to make a queries ?

import requests
link = "" #your link

myobj = {'somekey': 'somevalue'}

r = requests.post(link, data=myobj)
r.status_code
CODPUD
  • 11
  • 5
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.

Boomer
  • 493
  • 3
  • 9
  • I tried with request library and i got the 404 {"error":"Invalid api id. Verify your subdomain parameter"} in the response. – Rahul Nov 20 '20 at 21:46
  • What domain are you trying to pass? Do you need to read more on [requests](https://stackoverflow.com/questions/8685790/adding-headers-to-requests-module)? Update your question if your code/question is changing, please. – Boomer Nov 20 '20 at 21:51
  • curl https://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"} – Rahul Nov 20 '20 at 21:59
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.

CowboyTim
  • 47
  • 4