Gamunji Music
Gamunji Music

Reputation: 21

Python requests JSON parsing : JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Im having this problem with a profile generator im making ..

This is my code:

    print('Reading profile data')
    with open('profile_data.json') as file:
        data = json.load(file)
    print('Profile data loaded')
    task = int(raw_input('Task Number: '))
    delay = int(raw_input('Delay (second): '))
    x = 0
    while x < task:
        x = x + 1
        randomn = random.randint(10000, 100000000)
        email2, domain = data['email'].split('@')
        email1 = email2+'+'+str(randomn)+'@'+domain
        rando1 = random.randint(15, 40)
        r = requests.post('https://forms.nvidia.eu/FP_20161221_GFN_Early_Access/process?isJs=1&returnAs=json', data={'First_Name': data['fname'], 'Last_Name': data['lname'], 'email': email1, 'Platform': 'PC', 'Age': rando1, 'region': 'en_us'})
        e = requests.get('https://forms.nvidia.eu/FP_20161221_GFN_Early_Access/process?isJs=1&returnAs=json')
        print(e)
        succ = str(r.json()['status'])
        print(succ)
        if succ == 'SUCCESS':
            print('Success! '+str(x)+' Emails signed up')
            time.sleep(delay)
        elif succ == 'VERIFY':
            print('Task '+str(x)+' Failed due to captcha. Try again later, or use proxies')
            quit()
        else:
            print('Task number '+str(x)+' failed. Trying again in '+str(delay)+' seconds.')
            time.sleep(delay)

This is the full log, including the error:

Reading profile data
Profile data loaded
Task Number: 30
Delay (second): 0
<Response [501]>
SUCCESS
Success! 1 Emails signed up
<Response [501]>
SUCCESS
Success! 2 Emails signed up
<Response [501]>
SUCCESS
Success! 3 Emails signed up
<Response [501]>
SUCCESS
Success! 4 Emails signed up
<Response [501]>
SUCCESS
Success! 5 Emails signed up
<Response [501]>
SUCCESS
Success! 6 Emails signed up
<Response [501]>
SUCCESS
Success! 7 Emails signed up
<Response [501]>
SUCCESS
Success! 8 Emails signed up
<Response [503]>
SUCCESS
Success! 9 Emails signed up
<Response [503]>
SUCCESS
Success! 10 Emails signed up
<Response [501]>
Traceback (most recent call last):
  File "main.py", line 47, in <module>
    succ = str(r.json()['status'])
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/models.py", line 894, in json
    return complexjson.loads(self.text, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/simplejson/__init__.py", line 516, in loads
    return _default_decoder.decode(s)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/simplejson/decoder.py", line 370, in decode
    obj, end = self.raw_decode(s)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/simplejson/decoder.py", line 400, in raw_decode
    return self.scan_once(s, idx=_w(s, idx).end())
simplejson.scanner.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Macs-MBP:GeforceNow Mac$ 

I have tried browsing other questions on this site, but found none that were similar to my problem. You cant directly access the link to view the json data (at least i dont think you can..). This is probably a very simple question, but ill ask anyway (sorry if it is). Any help is appreciated, thank you! :)

Upvotes: 2

Views: 2696

Answers (1)

Chris May
Chris May

Reputation: 822

It looks like this line has an issue with it:

r = requests.post('https://forms.nvidia.eu/FP_20161221_GFN_Early_Access/process?isJs=1&returnAs=json', data={'First_Name': data['fname'], 'Last_Name': data['lname'], 'email': email1, 'Platform': 'PC', 'Age': rando1, 'region': 'en_us'})

According to the docs, when you're passing in a dictionary to the post, you should use the json parameter instead of the data parameter.

I think that will help. It's hard to say, since in your case the server is returning errors (501 Not Implemented and 503 Service Unavailable), but the code is acting as though they are successes. Successful server transactions should return codes in the 200 range.

Upvotes: 1

Related Questions