How to pass basic authentication using REST API in Confluence
Asked by Aashutosh Kumar:

Hi All,

I am trying to use REST API Calls to Confluence (Data Center version), but cannot figure out how to pass basic authentication in request call.

Below is example code taken from Confluence REST API Documentation.

```
import requests 
import json 
url = "https://your-domain.atlassian.net/wiki/rest/api/content" 
headers = { "Accept": "application/json" } 
response = requests.request( "GET", url, headers=headers ) print(json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": ")))
```

Below is the python request module code I am using.
>
> ```
> import requests
> import json
> from requests.auth import HTTPBasicAuth
> auth = HTTPBasicAuth("username","password")
> url = "http://confluenceurl:8090/confluence/rest/api/content"
> 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=(",", ": ")))
>
> But, I get 404 Response code
> (404=Returned if the calling user does not have permission to view the requested content.A schema has not been defined for this response code.)
> ```

Thanks in Advance for helping and teaching.


Answers:
author: Prince Nyeche
body: Hi [@Aashutosh Kumar](/t5/user/viewprofilepage/user-id/4006773)

Your headers has to be content-type.you can simply use below.

```
import requests
import json
from requests.auth import HTTPBasicAuth
auth = HTTPBasicAuth("username","password")
url = "http://confluenceurl:8090/confluence/rest/api/content"
headers = {"Content-type": "application/json"}
response = requests.request("GET",url,headers=headers,auth=auth)
print(json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": ")))


```

comments: author: Aashutosh Kumar
body: Hi [@Prince Nyeche](/t5/user/viewprofilepage/user-id/1928483) ,

Thank you the response, I modified header data, still same response.

```
>>> headers = {"Content-type": "application/json"}
>>> requests.request("GET",url,headers=headers,auth=('user','passwd'))
<Response [404]>
>>>
```

```
>>> headers = {"Content-type": "application/json"}
>>> requests.request("GET",url,headers=headers,auth=auth)
<Response [404]>
>>>
```


author: Prince Nyeche
body: I think the issue is not the authentication but most probably the endpoint being used. Is that endpoint url accessible via the browser?


author: Aashutosh Kumar
body: No, I tried below two urls.

<http://jirasdconfluence.technicolor.com:8090/confluence/rest/api/content>  
<http://jirasdconfluence.technicolor.com:8090/wiki/rest/api/content>  

I am using Confluence Data Center on own premises.

<br />

Page Not Found.

![url.PNG](https://community.atlassian.com/t5/image/serverpage/image-id/124754i4D6DF316E4E4EF29/image-size/large?v=v2&px=999 "url.PNG")


author: Prince Nyeche
body: There's the problem. the endpoint doesn't search any space. Please use

```
http://jirasdconfluence.technicolor.com:8090/wiki/rest/api/content/search?cql=space=NAME_OF_A_SPACE
```

This is explained here on [Advanced searching](https://developer.atlassian.com/server/confluence/advanced-searching-using-cql/) with CQL.


author: Aashutosh Kumar
body: Hi [@Prince Nyeche](/t5/user/viewprofilepage/user-id/1928483) ,

I am still unable to understand, which url should i make call using REST API.  
I want to fetch some data or post any data to any space/page in Confluence using REST API, but don't know how to pass user authentication.  

None of the URLs are working.

I am using Confluence Data Center Version.

```
>>>url='http://jirasdconfluence.technicolor.com:8090/wiki/rest/api/content/search?cql=space=TECHPST'
>>> headers = {"Accept": "application/json"}
>>> requests.request("GET",url,headers=headers,auth=auth)
<Response [404]>
>>>

![image.png](https://community.atlassian.com/t5/image/serverpage/image-id/124985i9E7A73687C9AD4CA/image-size/large?v=v2&px=999 "image.png")
```

<br />


author: Prince Nyeche
body: Hi [@Aashutosh Kumar](/t5/user/viewprofilepage/user-id/4006773)

I think you're using the directory wrong. If your confluence is not installed in any directory, it should be like

```
http://jirasdconfluence.technicolor.com:8090/rest/api/content/search?cql=space=TECHPST'
```

"wiki" -\> is a directory mostly used by Cloud Infrastructure for confluence. So try the above url and see if you can get the content of that space.


author: Aashutosh Kumar
body: It worked, Thank you so much [@Prince Nyeche](/t5/user/viewprofilepage/user-id/1928483) .


Tags: api

confluence-server

python

request

rest

rest-api

server