Office365-REST-Python-Client
Office365-REST-Python-Client copied to clipboard
I am getting json issue while uploading a file
Hello,
I am trying to upload a file to sharepoint but I am getting some json error which I figure out. Someone please help me in solving this.
code:
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
import os
baseurl = 'https://<company>.sharepoint.com'
basesite = '/personal/xyz/Documents/Documents'
siteurl = baseurl + basesite
localpath = "index2.py"
remotepath = "/personal/xyz/Documents/Documents/index2.py"
ctx_auth = AuthenticationContext(baseurl)
ctx_auth.acquire_token_for_user(username, password)
ctx = ClientContext(siteurl, ctx_auth)
with open(localpath, 'rb') as content_file:
file_content = content_file.read()
dir, name = os.path.split(remotepath)
file = ctx.web.get_folder_by_server_relative_url(dir).upload_file(name, file_content).execute_query()
I am getting below error
Traceback (most recent call last):
File "C:\Program Files\Python310\lib\site-packages\requests\models.py", line 971, in json
return complexjson.loads(self.text, **kwargs)
File "C:\Program Files\Python310\lib\json\__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "C:\Program Files\Python310\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Program Files\Python310\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "\\cntr\workstationdfs\RedirectedFolders$\n080892\Desktop\S3-onedrive\index5.py", line 20, in <module>
file = ctx.web.get_folder_by_server_relative_url(dir).upload_file(name, file_content).execute_query()
File "C:\Program Files\Python310\lib\site-packages\office365\runtime\client_object.py", line 52, in execute_query
self.context.execute_query()
File "C:\Program Files\Python310\lib\site-packages\office365\runtime\client_runtime_context.py", line 142,
in execute_query
self.pending_request().execute_query()
File "C:\Program Files\Python310\lib\site-packages\office365\runtime\client_request.py", line 80, in execute_query
self.beforeExecute.notify(request)
File "C:\Program Files\Python310\lib\site-packages\office365\runtime\types\event_handler.py", line 21, in notify
listener(*args, **kwargs)
File "C:\Program Files\Python310\lib\site-packages\office365\sharepoint\client_context.py", line 260, in _build_modification_query
self.ensure_form_digest(request)
File "C:\Program Files\Python310\lib\site-packages\office365\sharepoint\client_context.py", line 175, in ensure_form_digest
self._ctx_web_info = self.get_context_web_information(request_options=request_options)
File "C:\Program Files\Python310\lib\site-packages\office365\sharepoint\client_context.py", line 186, in get_context_web_information
json = response.json()
File "C:\Program Files\Python310\lib\site-packages\requests\models.py", line 975, in json
raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Also experiencing a similar issue when installing the most recent version from git.
i'm getting the same error
Getting almost the same error. I'm trying to download files from Sharepoint and then I get this: "requests.exceptions.JSONDecodeError: Expecting value: line 2 column 1 (char 2)".
same here
Same problem here.
@vgrem, can you check it out?
Here is sample code, using your example:
import requests
import csv
import os
from bs4 import BeautifulSoup
from office365.sharepoint.client_context import ClientContext
from office365.runtime.auth.client_credential import ClientCredential
FILE_PATH = 'test_file.csv'
# Get sample data
r = requests.get('https://developers.google.com/public-data/docs/canonical/countries_csv')
soup = BeautifulSoup(r.text, 'html.parser')
# Get the table with the data we want
table_body = soup.find_all('table')[0]
rows = table_body.find_all('tr')
# Create a new file and add the table data
with open(FILE_PATH, mode='w', newline='') as file:
# Create the csv writer
writer = csv.writer(file)
# File header
header = ['country', 'latitude', 'longitude', 'name']
writer.writerow(header)
# File rows
for row in rows:
row_elements = row.find_all('td')
row_elements = [element.text.strip() for element in row_elements]
row_data = [element for element in row_elements if element]
if row_data != []:
writer.writerow(row_data)
client_credentials = ClientCredential(CLIENT_ID, CLIENT_SECRET)
client = ClientContext(SITE_URL).with_credentials(client_credentials)
with open(FILE_PATH, 'rb') as content_file:
file_content = content_file.read()
list_title = "Documents"
target_folder = client.web.lists.get_by_title(list_title).root_folder
name = os.path.basename(FILE_PATH)
target_file = target_folder.upload_file(name, file_content).execute_query()
print("File has been uploaded to url: {0}".format(target_file.serverRelativeUrl))
If seems requests is trying to post it as 'json' and not as 'data':
JSONDecodeError Traceback (most recent call last)
File [c:\sharepoint\.venv\lib\site-packages\requests\models.py:971, in Response.json(self, **kwargs)
970 try:
--> 971 return complexjson.loads(self.text, **kwargs)
972 except JSONDecodeError as e:
973 # Catch JSON-related errors and raise as requests.JSONDecodeError
974 # This aliases json.JSONDecodeError and simplejson.JSONDecodeError
Files\Python39\lib\json\__init__.py:346, in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
343 if (cls is None and object_hook is None and
344 parse_int is None and parse_float is None and
345 parse_constant is None and object_pairs_hook is None and not kw):
--> 346 return _default_decoder.decode(s)
347 if cls is None:
Files\Python39\lib\json\decoder.py:337, in JSONDecoder.decode(self, s, _w)
333 """Return the Python representation of ``s`` (a ``str`` instance
334 containing a JSON document).
335
336 """
--> 337 obj, end = self.raw_decode(s, idx=_w(s, 0).end())
338 end = _w(s, end).end()
Files\Python39\lib\json\decoder.py:355, in JSONDecoder.raw_decode(self, s, idx)
...
973 # Catch JSON-related errors and raise as requests.JSONDecodeError
974 # This aliases json.JSONDecodeError and simplejson.JSONDecodeError
--> 975 raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
JSONDecodeError: Expecting value: line 1 column 1 (char 0)
I have the same error when I am trying to upload the data to the SharePoint using .execute_query()
raise JSONDecodeError("Extra data", s, end) json.decoder.JSONDecodeError: Extra data: line 1 column 5 (char 4)
Similar intermitent error here while uploading files to sharepoint:
File "/home/airflow/.local/lib/python3.7/site-packages/office365/sharepoint/client_context.py", line 133, in request_form_digest json = response.json() File "/home/airflow/.local/lib/python3.7/site-packages/requests/models.py", line 975, in json raise RequestsJSONDecodeError(e.msg, e.doc, e.pos) requests.exceptions.JSONDecodeError: Extra data: line 1 column 5 (char 4)
Has a fix been found for this? @vgrem
Facing a similar issue when downloading from sharepoint in a docker container.
Hi,
Not yet. Sorry. It's an intermitent issue so we manage through retrying
De: RohitMidha23 @.> Enviado: sábado, 23 de septiembre de 2023 10:25 a. m. Para: vgrem/Office365-REST-Python-Client @.> Cc: cframirezg @.>; Comment @.> Asunto: Re: [vgrem/Office365-REST-Python-Client] I am getting json issue while uploading a file (Issue #547)
Has a fix been found for this? @vgremhttps://github.com/vgrem
— Reply to this email directly, view it on GitHubhttps://github.com/vgrem/Office365-REST-Python-Client/issues/547#issuecomment-1732341981, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ABBPNTJEHOQY33NOJVGX6X3X335NPANCNFSM55RDZRBQ. You are receiving this because you commented.Message ID: @.***>
News?