Skip to content

RequestsDocumentLoader

RequestsDocumentLoader retrieves JSON-LD documents with requests.

The default remote document loader uses requests when it is available. Production applications should usually set at least a timeout:

Example requests_timeout.py

import json

from pyld import RequestsDocumentLoader, jsonld

doc = {
    "@context": {"name": "http://schema.org/name"},
    "name": "Earth",
}

loader = RequestsDocumentLoader(timeout=10)
result = jsonld.expand(doc, options={"documentLoader": loader})
print(json.dumps(result, indent=2))
Output
[
  {
    "http://schema.org/name": [
      {
        "@value": "Earth"
      }
    ]
  }
]

Use secure=True to require HTTPS URLs:

Example requests_secure.py

import json

from pyld import RequestsDocumentLoader, jsonld

doc = {
    "@context": {"name": "http://schema.org/name"},
    "name": "Earth",
}

loader = RequestsDocumentLoader(secure=True, timeout=10)
result = jsonld.expand(doc, options={"documentLoader": loader})
print(json.dumps(result, indent=2))
Output
[
  {
    "http://schema.org/name": [
      {
        "@value": "Earth"
      }
    ]
  }
]

Extra keyword arguments are forwarded to requests.get():

Example requests_extra_kwargs.py

import json

from pyld import RequestsDocumentLoader, jsonld

doc = {
    "@context": {"name": "http://schema.org/name"},
    "name": "Earth",
}

loader = RequestsDocumentLoader(
    timeout=10,
    verify=True,
    cert=("client.crt", "client.key"),
)
result = jsonld.expand(doc, options={"documentLoader": loader})
print(json.dumps(result, indent=2))
Output
[
  {
    "http://schema.org/name": [
      {
        "@value": "Earth"
      }
    ]
  }
]

Install the optional dependency with:

pip install "PyLD[requests]"