Download file from GitLab using Python HTTP requests - python

I'd like to download a YAML file from Gitlab using Python requests. I'm almost there, but I havent quite got to the cigar stage.
I am doing the following :-
GITLAB_FILE ="https://my_url/api/v4/projects/my_id/repository/files/path_and_filename.yaml/raw?ref=master&private_token=mytoken"
g=requests.request("GET", GITLAB_FILE, verify=False)
print(g.json())
Now, it all works in as much as I can get to the file ok, but when it comes to accessing the data the print(g.json()) throws an error, but then continues to print out the file contents as I'd hoped. The error is :-
Traceback (most recent call last):
File "/home/myproj/edm/lib/python3.7/site-packages/requests/models.py", line 910, in json
return complexjson.loads(self.text, **kwargs)
File "/usr/lib/python3.7/json/__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.7/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.7/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 "./http_test.py", line 73, in <module>
print (g.json())
File "/home/myproj/edm/lib/python3.7/site-packages/requests/models.py", line 917, in json
raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
requests.exceptions.JSONDecodeError: [Errno Expecting value]
Once this is printed, it then proceeds to print out the contents of my yaml file correctly.
I suspect its something to do with the
print(g.json())
expecting a json format, and encountering a yaml file?
Any pointers as to how I can get the file contents error free would be helpful.

I've answered my own question.
Quite simply instead of
print(g.json())
I used
print(g.text)
Which did exactly what it said on the tin.

Related

json.decoder.JSONDecodeError while splitting a string

I was trying to check if a string is JSON or not. If a string is not JSON string, I am trying to split the string (based on delim ":"). But it is throwing json.decoder.JSONDecodeError. I must be doing some silly mistake. But unable to figure out the issue.
Below is the simple program.
import json
str = "SIP/2.0 200"
try:
json.loads(str)
except ValueError as err:
substr = str.split((':', 1))
Below is the error
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "/usr/lib64/python3.7/json/__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "/usr/lib64/python3.7/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib64/python3.7/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 "<stdin>", line 4, in <module>
TypeError: must be str or None, not tuple
If I just print the string without splitting, it is working fine as below.
import json
str = "SIP/2.0 200"
try:
json.loads(str)
except ValueError as err:
print(str)
print(type(str))
O/P:
SIP/2.0 200
<class 'str'>
Not sure why I am getting this error. I might be doing some simple mistake. But unable to figure out the issue. Can anyone please help me to understand why splitting is failing.

Viusal studio python error when using requests package (error : json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0))

Im getting the error whenever i try to run this code in visual studio code.
Image of code
Error message:
Traceback (most recent call last):
File "c:\Users\climia1\Downloads\hangman.py", line 3, in <module>
r = requests.get('https://api.hypixel.net/skyblock/bazaar?key=29d9ecf6-71bb-49b1-b931-e0ed5f00f31d').json()
File "C:\Users\climia1\AppData\Local\Programs\Python\Python37\lib\site-packages\requests\models.py", line 898, in json
return complexjson.loads(self.text, **kwargs)
File "C:\Users\climia1\AppData\Local\Programs\Python\Python37\lib\json\__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "C:\Users\climia1\AppData\Local\Programs\Python\Python37\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\climia1\AppData\Local\Programs\Python\Python37\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)
[Done] exited with code=1 in 18.946 seconds
it displays the same error on repl.it but it goes away after one run. with visual studio it stays. any help?
Your json file must be empty or isn't formatted properly
if you are reading or writing to a empty Json file make sure it contains {}
if you are writing or reading a json file with lot of data make sure all the tags are closed

Problem of Python running code error on Pycharm

I'm using Pycharm professional 2019.03 to execute Python code Streaming Twitter Filter. The running code finish unsuccessfully with message error :
Traceback (most recent call last):
File "C:/Users/HP/PycharmProjects/Twitter_Stream/twitter_filter.py", line 12, in <module>
with open(file_path) as f:
FileNotFoundError: [Errno 2] No such file or directory: '/config/api.json'
When I ran the script print(os.getcwd()) the result is C:\Users\HP\PycharmProjects\Twitter_Stream
The Run/Debug configurations Windows show that the working directory is
C:\Users\HP\PycharmProjects\Twitter_Stream and all seem ok but I dont know why this message error. The api.json file is in the same directory than twitter_filter.py
I tried to replace the code :
file_path = '/config/api.json' by file_path = '/Users/HP/PycharmProjects/Twitter_Stream/config/api.json' or
file_path = 'C:/Users/HP/PycharmProjects/Twitter_Stream/config/api.json',
I have this message error :
Traceback (most recent call last):
File "C:/Users/HP/PycharmProjects/Twitter_Stream/twitter_filter.py", line 13, in <module>
twitter_api = json.loads(f.read())
File "C:\Users\HP\Anaconda3\lib\json\__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "C:\Users\HP\Anaconda3\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\HP\Anaconda3\lib\json\decoder.py", line 353, in raw_decode
obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 3 column 5 (char 7)
Please someone can help me to solve thise problem? Let me know if you need more information
Thank you !
(I would comment but I do not have enough rep points.)
When you see the JSONDecodeError, it means the JSON file is successfully being read by the module.
However, it appears the module deems the file invalid.
From past experiences, I would check if the JSON file is valid with this link: https://jsonformatter.curiousconcept.com/
I think what could be wrong is that your JSON file is using single quotes instead of double quotes, so not properly formatted.

I have a problem with json variable setting

i have this code here:
import json
with open("pass_file.txt", "r") as file:
password = json.loads(file.read())
it calls this error:
Traceback (most recent call last):
File "testdoc.py", line 9, in <module>
print(json.loads(file.read()))
File "C:\Program Files\Python37\lib\json\__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "C:\Program Files\Python37\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Program Files\Python37\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)
I dont know why this is happening because i have the same code on another file just with different variable name and file name and it works file. I did notice another similar question about a similar error but it didnt answer my question.
Thanks in advance :)
What is the content of you pass_file.txt ? The python code use json.loads so it expect JSON formated content in the pass_file.txt
For example for a string, the content of this file will be "hello world"
If you don't put quotes, the JSON parsing process will fail.

Python file that has always worked suddenly getting json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

This file has worked every time I've run it every day until today. Based on the error, here's what I think is the relevant code:
LastFM_artistMBID = MusicBrainz_artistMBID
get_artist_info_from_LastFM = lastFM.makeGetArtistInfoFromLastFM_URL(LastFM_artistMBID)
artist_info_from_LastFM = requests.get(get_artist_info_from_LastFM)
artistData = json.loads(artist_info_from_LastFM.text)
That last line is what the error(s) at the bottom seem to hate.
The MusicBrainz_artistMBID variable in the first line above is an item from the mbid_array2 list below. All of the above code is part of the get_artists_data function below. If you need more code, I can edit this answer.
for mbid in artistsData.mbid_array2:
get_artists_data(mbid)
Errors (including the first line from a print command while debugging):
<Response [200]>
Traceback (most recent call last):
File "getArtistDataCron.py", line 216, in <module>
get_artists_data(mbid)
File "getArtistDataCron.py", line 54, in get_artists_data
artistData = json.loads(artist_info_from_LastFM.text)
File "/home/bombybomb/anaconda3/lib/python3.6/json/__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "/home/bombybomb/anaconda3/lib/python3.6/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/home/bombybomb/anaconda3/lib/python3.6/json/decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
I've tried several solutions and suggestions I've found from Googling but nothing seems to fix it. This file worked fine every time until today.
Expecting value: line 1 column 1 (char 0) means that the given value is entirely empty. Chances are the server is responding with no content at all due to a problem on the server end. You would just have to wait until the issues on the server are resolved if you have no access to fixing the server issues.

Categories