Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Upload XML as attachment to confluence using REST API and pyCurl

Aby12345 July 5, 2018 edited

Hello Brethren,

I am trying to upload an xml file to confluence REST API using pyCurl as below. While using this I get a 400 Bad request error from the server. My other pyCurl code to update the page works fine with the REST API. Please help me resolve. Below is the code

def upload_files_to_confluence(pageId,filepath):
    b = io.BytesIO()
    c = pycurl.Curl()
    url= config['RESTURLS']['CONFURL'] +"rest/api/content/"+pageId+"/child/attachment"
    c.setopt(pycurl.URL, url)
    c.setopt(pycurl.WRITEFUNCTION, b.write)
    c.setopt(pycurl.CAINFO, certifi.where())
    c.setopt(pycurl.PROXY, config['CERTIFICATES']['PROXY'])
    c.setopt(pycurl.SSLKEY, config['CERTIFICATES']['SSLKEYPATH'])
    c.setopt(pycurl.SSLCERT, config['CERTIFICATES']['SSLCERTPATH'])
    c.setopt(pycurl.USERPWD, config['AUTH']['USERNAME'] + ':' + config['AUTH']['PASSWORD'])
    c.setopt(pycurl.POST,1)
    c.setopt(pycurl.HTTPHEADER,['Content-type: text/xml; charset=UTF-8','X-Atlassian-Token : no-check','Accept-Language: en'])
    c.setopt(pycurl.HTTPPOST, [("testResults", (c.FORM_FILE, filepath,pycurl.FORM_CONTENTTYPE,"text/xml",pycurl.FORM_FILENAME, "testResult1"))])
    c.setopt(c.VERBOSE, True)
    c.perform()
    c.close()
    response_body = b.getvalue().decode('UTF-8')
    print(response_body)

1 answer

0 votes
Gonchik Tsymzhitov
Community Champion
September 8, 2018

Hi! 

 

Feel free reuse this method or library

https://github.com/AstroMatt/atlassian-python-api/blob/master/atlassian/confluence.py#L256

 

 

Cheers,

Gonchik Tsymzhitov

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events