简体   繁体   中英

Python - Value Error on REST Api call using requests

I am trying to check my video upload to vimeo in Python but I keep getting the error:

ValueError: HTTP request does not contain valid JSON data

I have it working in postman with the the same headers and body set to none. This is what I get returned in postman:

{
    "uri": "/videos/*********",
    "upload": {
        "status": "complete"
    },
    "transcode": {
        "status": "complete"
    }
}

This is the link to the Vimeo Api request documentation:

https://developer.vimeo.com/api/upload/videos#table-3

This is what I am working with (Token is my bearer token and videoCode is the video id of Vimeo - sorry can't disclose those as their are from a client), request_url same as in postman, as are the headers.

payload = {}

json_object = json.dumps(payload)

def checkVimeoStatus(videoCode):
    request_url = f"https://api.vimeo.com/videos/{videoCode}?fields=uri,upload.status,transcode.status"
    headers = {'Authorization' : token, 'Connection': 'keep-alive', 'Accept' : '*/*', 'Accept-Encoding': 'gzip, deflate'}    
    response = requests.post(request_url, data=json_object, headers=headers)
    response.raise_for_status()
    results = response.json()
    return results

status = checkVimeoStatus(videoCode)

what am I doing wrong here, adding fake data does not seem to help as setting data=none as well? thanks for the help

error message:

System.Private.CoreLib: Exception while executing function: Functions.vimeoStatus. System.Private.CoreLib: Result: Failure
Exception: ValueError: HTTP request does not contain valid JSON data
Stack:   File "\azure-functions-core-tools\bin\workers\python\3.9/WINDOWS/X64\azure_functions_worker\dispatcher.py", line 402, in _handle__invocation_request
    call_result = await self._loop.run_in_executor(
  File "C:\Python39\lib\concurrent\futures\thread.py", line 52, in run
    result = self.fn(*self.args, **self.kwargs)
  File "python\3.9/WINDOWS/X64\azure_functions_worker\dispatcher.py", line 611, in _run_sync_func
    return ExtensionManager.get_sync_invocation_wrapper(context,
  File "\azure-functions-core-tools\bin\workers\python\3.9/WINDOWS/X64\azure_functions_worker\extension.py", line 215, in _raw_invocation_wrapper
    result = function(**args)
  File "..\vimeoStatus\__init__.py", line 9, in main
    req_body = req.get_json()
  File "\azure-functions-core-tools\bin\workers\python\3.9/WINDOWS/X64\azure\functions\http.py", line 61, in get_json
    raise ValueError(

requests will JSON-encode data for you when you use the json=... parameter ( see ), you don't need json.dumps() .

But you should set the Content-Type header. And you should use function parameters instead of global variables.

def checkVimeoStatus(videoCode, payload):
    response = requests.post(
        f'https://api.vimeo.com/videos/{videoCode}?fields=uri,upload.status,transcode.status',
        headers={
            'Authorization' : token,
            'Connection': 'keep-alive',
            'Accept' : '*/*',
            'Accept-Encoding': 'gzip, deflate',
            'Content-Type': 'application/json'
        },
        json=payload
    )
    response.raise_for_status()
    return response.json()

status = checkVimeoStatus(videoCode, {})

Depending on what the Vimeo API expects here, you might have to send a little more than {} . Also I doubt that Connection: keep-alive is useful here, and if you expect a JSON response, Accept: */* should be Accept: application/json .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM