Fixed post request in example

Paul Hecker 2023-07-14 21:44:32 +02:00
parent 8567147075
commit 46766660dc

@ -8,24 +8,26 @@ A more advanced post consume script which uses Python and the API to assign some
#!/usr/bin/env python3 #!/usr/bin/env python3
import os
import json
# TODO: The user can use anything in the standard library, installed for paperless # TODO: The user can use anything in the standard library, installed for paperless
# or use the custom startup scripts to install additional libraries via pip # or use the custom startup scripts to install additional libraries via pip
import requests import requests
import os
def _set_auth_tokens(paperless_url: str, timeout: float, session: requests.Session): def _set_auth_tokens(paperless_url: str, timeout: float, session: requests.Session):
# TODO: You fill these in or otherwise provide them # TODO: You fill these in or otherwise provide them
credentials = {"username": "test", "password": "test"} credentials = {"username": "test", "password": "test"}
response = sess.get(paperless_url, timeout=timeout) response = session.get(paperless_url, timeout=timeout)
response.raise_for_status() response.raise_for_status()
csrf_token = response.cookies["csrftoken"] csrf_token = response.cookies["csrftoken"]
response = sess.post( response = session.post(
paperless_url + "/api/token/", paperless_url + "/api/token/",
data=credentials, data=json.dumps(credentials),
headers={"X-CSRFToken": csrf_token}, headers={"X-CSRFToken": csrf_token},
timeout=timeout, timeout=timeout,
) )
@ -73,7 +75,7 @@ if __name__ == "__main__":
# Update the document # Update the document
resp = sess.patch( resp = sess.patch(
paperless_url + f"/api/documents/{doc_pk}/", paperless_url + f"/api/documents/{doc_pk}/",
data={"correspondent": new_correspondent, "document_type": new_doc_type}, data=json.dumps({"correspondent": new_correspondent, "document_type": new_doc_type}),
timeout=timeout, timeout=timeout,
) )
resp.raise_for_status() resp.raise_for_status()