Hello, I'm not sure where I should post this issue, in the client or in the doccano repository. So if it's the wrong place please tell me that and I'll move it.
from doccano_api_client import DoccanoClient
> doccano_client = DoccanoClient(
> os.environ["doccano_api_host"],
> os.environ["doccano_api_login"],
> os.environ["doccano_api_password"]
>)
> doccano_client.get_me() # <- GET request, ok
{'id': 1, 'username': 'admin', 'is_superuser': True}
> doccano_client.get_document_list(1) # <- GET request, ok
{'count': 0, 'next': None, 'previous': None, 'results': []}
> doccano_client.post_approve_labels(1, 1)
{'detail': 'CSRF Failed: CSRF token missing or incorrect.'}
And a more interesting case for the document upload. It doesn't handle the correct JSON error but sends HTML page and the client raises an exception:
>r = doccano_client.post_doc_upload(
> 1,
> file_format='json',
> file_name='fl.json',
> file_path=doccano_input_dir)
---------------------------------------------------------------------------
JSONDecodeError Traceback (most recent call last)
<ipython-input-22-e1e0971d4839> in <module>
3 file_format='json',
4 file_name='fl.json',
----> 5 file_path=doccano_input_dir)
<path>/lib/python3.7/site-packages/doccano_api_client/__init__.py in post_doc_upload(self, project_id, file_format, file_name, file_path)
546 ),
547 files=files,
--> 548 data=data
549 )
550
<path>/lib/python3.7/site-packages/doccano_api_client/__init__.py in post(self, endpoint, data, json, files)
62 request_url = urljoin(self.baseurl, endpoint)
63 return self.session.post(
---> 64 request_url, data=data, files=files, json=json).json()
65
66 def delete(
<path>/lib/python3.7/site-packages/requests/models.py in json(self, **kwargs)
898 # used.
899 pass
--> 900 return complexjson.loads(self.text, **kwargs)
901
902 @property
<path>/lib/python3.7/json/__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
346 parse_int is None and parse_float is None and
347 parse_constant is None and object_pairs_hook is None and not kw):
--> 348 return _default_decoder.decode(s)
349 if cls is None:
350 cls = JSONDecoder
<path>/lib/python3.7/json/decoder.py in decode(self, s, _w)
335
336 """
--> 337 obj, end = self.raw_decode(s, idx=_w(s, 0).end())
338 end = _w(s, end).end()
339 if end != len(s):
<path>/lib/python3.7/json/decoder.py in raw_decode(self, s, idx)
353 obj, end = self.scan_once(s, idx)
354 except StopIteration as err:
--> 355 raise JSONDecodeError("Expecting value", s, err.value) from None
356 return obj, end
JSONDecodeError: Expecting value: line 2 column 1 (char 1)
data = {
'file': (
'fl.jsonl',
open(f"{doccano_input_dir}/fl.json", "r")
)
}
files = {
'file': (
'fl.jsonl',
open(f"{doccano_input_dir}/fl.json", "r")
)
}
url = 'v1/projects/{project_id}/docs/upload'.format(project_id=1)
request_url = doccano_client.baseurl + url
resp = doccano_client.session.post(request_url, data=data)
print(resp.text)
============
'\n<!DOCTYPE html>\n<html lang="en">\n<head>\n <meta http-equiv="content-type" content="text/html; charset=utf-8">\n <meta name="robots" content="NONE,NOARCHIVE">\n <title>403 Forbidden</title>\n <style type="text/css">\n html * { padding:0; margin:0; }\n body * { padding:10px 20px; }\n body * * { padding:0; }\n body { font:small sans-serif; background:#eee; color:#000; }\n body>div { border-bottom:1px solid #ddd; }\n h1 { font-weight:normal; margin-bottom:.4em; }\n h1 span { font-size:60%; color:#666; font-weight:normal; }\n #info { background:#f6f6f6; }\n #info ul { margin: 0.5em 4em; }\n #info p, #summary p { padding-top:10px; }\n #summary { background: #ffc; }\n #explanation { background:#eee; border-bottom: 0px none; }\n </style>\n</head>\n<body>\n<div id="summary">\n <h1>Forbidden <span>(403)</span></h1>\n <p>CSRF verification failed. Request aborted.</p>\n\n\n</div>\n\n<div id="info">\n <h2>Help</h2>\n \n <p>Reason given for failure:</p>\n <pre>\n CSRF token missing or incorrect.\n </pre>\n \n\n <p>In general, this can occur when there is a genuine Cross Site Request Forgery, or when\n <a\n href="https://docs.djangoproject.com/en/3.2/ref/csrf/">Djangoโs\n CSRF mechanism</a> has not been used correctly. For POST forms, you need to\n ensure:</p>\n\n <ul>\n <li>Your browser is accepting cookies.</li>\n\n <li>The view function passes a <code>request</code> to the templateโs <a\n href="https://docs.djangoproject.com/en/dev/topics/templates/#django.template.backends.base.Template.render"><code>render</code></a>\n method.</li>\n\n <li>In the template, there is a <code>{% csrf_token\n %}</code> template tag inside each POST form that\n targets an internal URL.</li>\n\n <li>If you are not using <code>CsrfViewMiddleware</code>, then you must use\n <code>csrf_protect</code> on any views that use the <code>csrf_token</code>\n template tag, as well as those that accept the POST data.</li>\n\n <li>The form has a valid CSRF token. After logging in in another browser\n tab or hitting the back button after a login, you may need to reload the\n page with the form, because the token is rotated after a login.</li>\n </ul>\n\n <p>Youโre seeing the help section of this page because you have <code>DEBUG =\n True</code> in your Django settings file. Change that to <code>False</code>,\n and only the initial error message will be displayed. </p>\n\n <p>You can customize this page using the CSRF_FAILURE_VIEW setting.</p>\n</div>\n\n</body>\n</html>\n'