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

  • Guy Shimon changed the title to making a GET request with Python

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

 Share

  • Recently Browsing   0 members

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