REST API Call 404 error

I am trying make a REST call to get current user details using sample code:

### Get current user
# This code sample uses the ‘requests’ library:

# http://docs.python-requests.org

import requests
from requests.auth import HTTPBasicAuth
import json

url = “https://sushrutikarthik.atlassian.net/wiki/rest/api/current”

auth = HTTPBasicAuth("sushruti.karthik@gmail.com",“api_token_here”)

headers = {
  “Accept”: “application/json”
}

response = requests.request(
  “GET”,
  url,
  headers=headers,
  auth=auth
)

print(json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": ")))

I get following error.

{
“message”: “null for uri: https://sushrutikarthik.atlassian.net/wiki/rest/api/current”,
“status-code”: 404
}

Can someone help. I am new to programming.

Regards,
Sushruti

Hi @sushrutikarthik - the correct URL to hit is:

/wiki/rest/api/user/current

Also, be sure that you’re using an API token… and not your actual password for your account. Password support in basic auth has been deprecated. Lastly, please be sure not to post your API token in messages.

1 Like

Thank you Mansilla. It worked. Also, now I am creating space using REST API and I get 400 error.

This is sample code I am using from the API docs.

This code sample uses the ‘requests’ library:

http://docs.python-requests.org

import requests
from requests.auth import HTTPBasicAuth
import json

url = “https://sushrutikarthik.atlassian.net/wiki/rest/api/space

auth = HTTPBasicAuth("email@gmail.com", “api-key”)

headers = {
“Accept”: “application/json”,
“Content-Type”: “application/json”
}

payload = json.dumps( {
“key”: “”,
“name”: “”,
“description”: {
“plain”: {
“value”: “”,
“representation”: “”
}
},
“permissions”: [
{
“subjects”: {
“user”: {
“results”: [
{
“type”: “known”,
“username”: “”,
“userKey”: “”,
“accountId”: “”,

          "email": "<string>",
          "publicName": "<string>",
          "profilePicture": {
            "path": "<string>",
            "width": 2154,
            "height": 2154,
            "isDefault": True
          },
          "displayName": "<string>",
          "operations": [
            {
              "operation": "administer",
              "targetType": "page"
            }
          ],
          "details": {},
          "personalSpace": {
            "id": 2154,
            "key": "<string>",
            "name": "<string>",
            "type": "<string>",
            "status": "<string>",
            "_expandable": {},
            "_links": {}
          },
          "_expandable": {
            "operations": "<string>",
            "details": "<string>",
            "personalSpace": "<string>"
          },
          "_links": {}
        }
      ],
      "size": 2154
    },
    "group": {
      "results": [
        {
          "type": "group",
          "name": "<string>",
          "_links": {}
        }
      ],
      "size": 2154
    },
    "_expandable": {
      "user": "<string>",
      "group": "<string>"
    }
  },
  "operation": {
    "operation": "administer",
    "targetType": "page"
  },
  "anonymousAccess": True,
  "unlicensedAccess": True
}

]
} )

response = requests.request(
“POST”,
url,
data=payload,
headers=headers,
auth=auth
)

print(json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": ")))

###Output I get is:
{
“data”: {
“authorized”: true,
“errors”: [
{
“message”: {
“args”: [],
“translation”: "Invalid space key: "
}
}
],
“successful”: false,
“valid”: false
},
“message”: “com.atlassian.confluence.api.service.exceptions.BadRequestException: Cannot create Space”,
“statusCode”: 400
}

Looks like example code has not been updated. Could you please guide me how to proceed in this area?