Search code examples
pythonpython-requestsputconfluence

Python PUT Request to Confluence Webpage - ValueError: No JSON object could be decoded, but <Response [200]>


I'm trying to update some data on a confluence webpage. Everything works fine in Postman (the data is updated). However, when I use python and the requests module I'm getting the following error:

ValueError: No JSON object could be decoded

The strangest thing is I'm getting a 200 status code back but the webpage isn't updating. The error seems to root from typing 'r.json'.

Here is my code (I'm trying to change the content of the webpage to 'Hello World'):

import requests
import json

url = <url>

data = {
    "id": "18219205",
    "title": "Testapi",
    "type": "page",
    "version": {
        "number": 11
    },
    "body": {
        "storage": {
            "representation": "storage",
            "value": "Hello world."
        }
    }
}
dumped_data = json.dumps(data)

headers = {
    'content-type': "application/json",
    'authorization': "Basic <token number>",
    'cache-control': "no-cache",
    'postman-token': "<another token>"
    }

r = requests.put(url, data=dumped_data, headers=headers, verify=False)

print r.json()

Solution

  • This is happening because the API you're posting to doesn't respond with JSON, so when you call r.json() requests tries to parse the body of the response as JSON and fails. You're seeing a 200 because you were able to send the data to the server correctly, you're just trying to read the response wrong.

    From the Requests docs:

    For example, if the response gets a 204 (No Content), or if the response contains invalid JSON, attempting r.json() raises ValueError: No JSON object could be decoded.