Home > Net >  Calling an API in visual studio code using Python
Calling an API in visual studio code using Python

Time:02-28

I am trying to call the marvel API from here: https://developer.marvel.com

The code I wrote for it:

import requests, json
from pprint import pprint

# Call an API
url = "http(s)://gateway.marvel.com/"
response = requests.get(url)
response.raise_for_status() # check for errors

# Load JSON data into a Python variable.
jsonData = json.loads(response.text)
pprint(jsonData)

When I run it, I receive the error : requests.exceptions.HTTPError: 409 Client Error: Conflict for url:

CodePudding user response:

It is from rest api

import requests
import json
from pprint import pprint

# Call an API
response = requests.get('https://jsonplaceholder.typicode.com/users')
response.raise_for_status()  # check for errors
print(response.status_code)
# Load JSON data into a Python variable.
jsonData = json.loads(response.text)
pprint(jsonData)

CodePudding user response:

I'm assuming the url you posted is just a placeholder? The correct format should be something like https://gateway.marvel.com:443/v1/public/characters?apikey=yourpublickey.

You also need the to specify an endpoint, in my example I used the /characters endpoint.

Try removing the response.raise_for_status(). It should continue and give you an error message with what is wrong (according to the docs, almost all errors will give you a status code of 409).

import requests, json
from pprint import pprint

# Call an API
url = "https://gateway.marvel.com:443/v1/public/characters?apikey=yourpublickey."
response = requests.get(url)
# response.raise_for_status() # check for errors

# Load JSON data into a Python variable.
jsonData = json.loads(response.text)
pprint(jsonData)
  • Related