xsrf check failed creating project bitbucket server using python request

Jeremie Labbe November 2, 2016

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

3 votes
Ulrich Kuhnhardt [IzymesDev]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 2, 2016

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

0 votes
Stan Ry
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 11, 2019

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'
}

 

0 votes
Jeremie Labbe November 3, 2016

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?

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events