HTTP Error 500 when passing a variable as URL

Viewed 36

When I pass a string as URL my code works fine but as soon as my URL is created by using a string and a variable I always get a error 500 back.

The code below gives me an error and {passID} has the same value as the code that works.

 response = requests.put(f'https://www.somesite.io/dir/{passID}', headers=headers, json=data)

The code below doesn't give me an error but I need the last part of the url to be dynamic.

response = requests.put(f'https://www.somesite.io/dir/ALaAfjflja', headers=headers, json=data)

I get the value for the {passID} by using a post method in another function:

def gettingID(longTime):
    url = "https://www.somelink.com/dir/"

headers = {"Accept": "application/json, text/plain, */*", "Accept-Encoding": "gzip, deflate", "Content-Type": "application/json; charset=utf-8",
    "Cookie": f"alksjdföasdf{longTime}"}
data = {"mode":1}

response = requests.post(url, headers=headers, json=data)

loadJson = (response.json())
y = json.dumps(loadJson["id"])
y = y.replace('"',"")
return(y)  
#y returns something like AKJHfdkhlsahakkA and works fine.

The exact error is:

Status Code 500
Traceback (most recent call last):
  File "/home/user/Documents/space/code/request.py", line 84, in <module>
    getStreak(newID, newIDDD, shortTime, endTime, longTime)
  File "/home/user/Documents/space/code/request.py", line 68, in getStreak
    print("JSON Response ", response.json())
  File "/usr/lib/python3/dist-packages/requests/models.py", line 897, in json
    return complexjson.loads(self.text, **kwargs)
  File "/usr/lib/python3/dist-packages/simplejson/__init__.py", line 518, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3/dist-packages/simplejson/decoder.py", line 373, in decode
    raise JSONDecodeError("Extra data", s, end, len(s))
simplejson.errors.JSONDecodeError: Extra data: line 1 column 5 - line 1 column 26 (char 4 - 25)

I will gladly share more code but maybe I'm making a dumb mistake and the issue is already at hand.

And here is the whole code:

from typing import Dict
import requests
import json
from yaml import load

shortTime = 164856200
endTime = shortTime + 3333
longTime = shortTime * 1000


def gettingID(shortTime):
    url = "https://www.somelink.com/dir/"

    headers = {"Accept": "application/json, text/plain, */*", "Accept-Encoding": "gzip, deflate", "Content-Type": "application/json; charset=utf-8",
        "Cookie": f"alksjdföasdf{shortTime}"}
    data = {"mode":1}

    response = requests.post(url, headers=headers, json=data)

    loadJson = (response.json())
    y = json.dumps(loadJson["id"])
    y = y.replace('"',"")
    return(y)  
    #y returns something like AKJHfdkhlsahakkA

def getStreak(passID, passIDD, shortTime):
    headers = {"Accept": "application/json, text/plain, */*", "Accept-Encoding": "gzip, deflate", "Content-Type": "application/json; charset=utf-8", "Cookie": f"alksjdföasdf{shortTime}"}
    data = {"mode":1}
    response = requests.put(f'https://www.somelink.com/dir/{passID}', headers=headers, json=data)

    print("Status Code", response.status_code)
    print("JSON Response ", response.json())

newID = gettingID(longTime)
#this var contains AKJHfdkhlsahakkA

#If I would pass var instead of newID the code works fine.
newIDD = "AKJHfdkhlsahakkA"
#this var has the same value as newID but written as a string.
#using this method works but the newID is always changing and I need it to be a variable so it dynamicly changes every time the code funs.

getStreak(newID, newIDD, shortTime, endTime, longTime)
0 Answers
Related