Jump to content

making a GET request with Python


Recommended Posts

Hi all,

I just read the API documentation, I only saw mentions of PHP. Did anyone managed to pull it with Python? I tried this. What am I missing?

import os
import json
import requests

API_KEY = "api_key"
ENDPOINT = "https://www.myforum.com"

headers = {
    'Authorization': 'Basic ' + API_KEY
}

response = requests.get(url=f"{ENDPOINT}/api/forums/forums", headers=headers)
response.raise_for_status()
print(response.text)

 

Edited by Guy Shimon
Link to comment
Share on other sites

I got a few 😀 first it was 401(unauthorized), then after fiddling with the parameters it was 429 (Client Error: Too Many Requests), and now it's :

requests.exceptions.ConnectionError: HTTPSConnectionPool(host='mywebsite', port=443): Max retries exceeded with url: /api/forums/forums (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x00000265DE369C40>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed'))

8 minutes ago, Daniel F said:

What was the response? Did you get any error?

I got a few 😀 first it was 401(unauthorized), then after fiddling with the parameters it was 429 (Client Error: Too Many Requests), and now it's :

requests.exceptions.ConnectionError: HTTPSConnectionPool(host='mywebsite', port=443): Max retries exceeded with url: /api/forums/forums (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x00000265DE369C40>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed'))

Link to comment
Share on other sites

Some very GENERAL notes regarding hitting the API. May or may not apply to you...

1) Friendly Urls enabled on your site? If not you need index.php? in your url call.

2) Is Python or your code setting a user agent? Doesn't need to be anything real, just exist. I've had servers tell me to get bent solely for lack of a user agent in the Curl call.

3) Shouldn't matter at all but when just starting out you can remove one variable by just poking the default API endpoint  - /core/hello - for the basic response. One less thing to have your mind on.

In your specific case are you closing that response call ever? That max retries error makes me wonder...

Good luck; I've got a tester built out but it's for PHP calls. Not much help for you right now.

Link to comment
Share on other sites

  • 5 months later...
20 hours ago, Stuart Silvester said:

As @All Astronauts points out, you will most definitely need to include a user-agent header when connecting to the REST API.

I did, did not help.

Is there anyone at Invision development team that knows Python? maybe just write the simplest API call and post it here? shouldn't be hard...

Link to comment
Share on other sites

I don't use Python, but it does look like your authentication header may not be formatted correctly. Don't forget basic headers are base64 encoded which your middleware typically does for you, but since you're building it yourself here, it's not the right format.

It looks like you could use auth=HTTPBasicAuth(user, password)

specifying the API key as the user, but I couldn't tell you if that would solve the issue.

Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...