Requests

This module provides functions to make HTTP requests

Example

from npc.requests import get, post, USER_AGENT_MAP

response = get("https://httpbin.org/get")
print(response.json)

response = get("https://httpbin.org/get", headers={"User-Agent": USER_AGENT_MAP["windows-chrome-126"]})
print(response.json)

response = post("https://httpbin.org/post", data=b"Hello, World!")
print(response.json)
npc.get(url, headers={}, timeout=30)

Make a GET request to the given URL

Parameters:
  • url (str) – URL to make the request to including the query parameters

  • headers (dict, optional) – Dict of headers to be sent with the request

  • timeout (int, optional) – Timeout for the request in seconds

Returns:

Response object

Return type:

Response

npc.post(url, data=None, headers={}, timeout=30)

Make a POST request to the given URL

Parameters:
  • url (str) – URL to make the request to including the query parameters

  • data (bytes | Iterable | npc.types.Readable, optional) – Raw bytes data to be sent with the requestrequest

  • headers (dict, optional) – Dict of headers to be sent with the request

  • timeout (int, optional) – Timeout for the request in seconds

Returns:

Response object

Return type:

Response

npc.requests.USER_AGENTS = ['Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.3', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:127.0) Gecko/20100101 Firefox/127.0', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.0', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.3', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 14_5) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.5 Safari/605.1.15', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 14.5; rv:127.0) Gecko/20100101 Firefox/127.0', 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36', 'Mozilla/5.0 (X11; Linux i686; rv:127.0) Gecko/20100101 Firefox/127.0', 'Mozilla/5.0 (iPhone; CPU iPhone OS 17_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/126.0.6478.108 Mobile/15E148 Safari/604.1', 'Mozilla/5.0 (iPhone; CPU iPhone OS 17_5_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.5 Mobile/15E148 Safari/604.1', 'Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Mobile Safari/537.3', 'Mozilla/5.0 (Android 14; Mobile; rv:127.0) Gecko/127.0 Firefox/127.0']

List of user agents to be used for making requests

npc.requests.USER_AGENT_MAP = {'android-chrome-126': 'Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Mobile Safari/537.3', 'android-firefox-127': 'Mozilla/5.0 (Android 14; Mobile; rv:127.0) Gecko/127.0 Firefox/127.0', 'linux-chrome-126': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36', 'linux-firefox-126': 'Mozilla/5.0 (X11; Linux i686; rv:127.0) Gecko/20100101 Firefox/127.0', 'mac-chrome-125': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.3', 'mac-firefox-127': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 14.5; rv:127.0) Gecko/20100101 Firefox/127.0', 'mac-safari-17': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 14_5) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.5 Safari/605.1.15', 'osx-chrome-126': 'Mozilla/5.0 (iPhone; CPU iPhone OS 17_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/126.0.6478.108 Mobile/15E148 Safari/604.1', 'osx-safari-17': 'Mozilla/5.0 (iPhone; CPU iPhone OS 17_5_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.5 Mobile/15E148 Safari/604.1', 'windows-chrome-126': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.3', 'windows-edge-126': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.0', 'windows-firefox-127': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:127.0) Gecko/20100101 Firefox/127.0'}

Map of user agent strings to be used for making requests

class npc.Response(obj)

Wrapper around HTTPResponse to provide additional functionality

Parameters:

obj – HTTPResponse object

mime_type

MIME type of the response

Type:

str

encoding

Encoding of the response

Type:

str

Properties:

raw (bytes): Raw response content content (str): Response content decoded with encoding json (dict | list): JSON response content

__getattr__(attr)

Get attribute from the wrapped object if not found in the current object

Parameters:

attr (str) – Attribute to get (e.g. status, headers, etc.

Returns:

Value of the attribute

Return type:

typing.Any

__repr__()

Return repr(self).

property raw: bytes

Raw response content

Returns:

Raw response

Return type:

bytes

property content: str

Response content decoded with encoding

Returns:

Decoded response content

Return type:

str

property json: Any

JSON response content

Returns:

JSON response content

Return type:

dict | list