It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

xsrf check failed creating project bitbucket server using python request

Hi Team,

I'm trying to create a project using a python script and the requests library.

When doing get requests, I have no problem, but when doing post requests, I encounter a "xsrf check failed"

Here's the snippet:

payload = {"key":"EQSS_TST", "name":"qtm_test"}

r = requests.post('http://server.co.uk:7990/rest/api/1.0/projects', params=payload, auth=(user,password))

The error message is "XSRF check failed"

Thanks for you help!

Jeremie

3 answers

The POST request header must contain Content-type = application/json 

I'm not planning on adding an avatar to the project so I shouldn't need to define the content-type.

The example prototype on the documentation says:

{
    "key": "PRJ",
    "name": "My Cool Project",
    "description": "The description for my cool project.",
    "avatar": "data:image/png;base64,<base64-encoded-image-data>"
}

Has anybody ever created a project using the "requests" python library?

I've had a similar issue with posting comments through response.post(), and it solved with adding the following lines to the header:

 'x-atlassian-token': 'no-check',
'x-requested-with': 'XMLHttpRequest',

 So the session object looks as follows:

s = requests.Session()
s.auth = auth
s.verify = False

s.headers = {
'origin': 'https://myserver.example.net',
'pragma': 'no-cache',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 YaBrowser/18.3.1.1220 Yowser/2.5 Safari/537.36',
'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
'referer': 'https://myserver.example.net/instance_name/pages/viewpage.action?pageId=123456789',
'x-atlassian-token': 'no-check',
'x-requested-with': 'XMLHttpRequest',
}

and response object looks as:

r = s.post(
'{}'.format(WEB),
params = params,
data = data,
)
print(r.text)

where

WEB = 'https://server.example.net/instance_name/pages/viewpage.action?pageId=123456789'

params = {'actions':'true'}

data = {
'html': '<p>Test</p>',
'watch': 'false',
'uuid': '54b022eb-e098-a3ad-5f8d-fd68619681fb'
}

 

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted in Bitbucket

Share your software development horror stories!

Hey Community! I work on the Bitbucket product marketing team. With Halloween approaching, we wanted to discuss a topic tailor-made for October: development horror stories. Whether it was a lurk...

1,492 views 11 3
Join discussion

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you