Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
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

How to export robotframework XML results using multipart and python requests with xray api?

Edited

Following this guide from Sergio found in stackoverflow I managed to get the simple upload version to work, but since we have mandatory fields such as "Components" it won't work.

I tried changing parts of the script following the xray documentation, but I just get 500 Internal Application Error when trying to run the script.

upload.py

import requests
import json

xray_cloud_base_url = "https://xray.cloud.xpand-it.com/api/v2"
client_id = "<my_id>"
client_secret = "<my_secret>"

headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
auth_data = { "client_id": client_id, "client_secret": client_secret }
response = requests.post(f'{xray_cloud_base_url}/authenticate', data=json.dumps(auth_data), headers=headers)
auth_token = response.json()

files = {
"info": ("issueFields.json", open("issueFields.json","rb")),
"testInfo": ("testIssueFields.json", open("testIssueFields.json","rb")),
"results": ("output.xml", open("results/output.xml","rb"))
}

headers = {'Authorization': 'Bearer ' + auth_token, 'Content-Type': 'multipart/form-data'}

response = requests.post(f'{xray_cloud_base_url}/import/execution/robot/multipart', headers=headers, files=files)

print(response.status_code)
print(response.content)
 
the result:
500
b'{"error":"Internal Application Error!"}'
My issueFields.json and testIssueFields.json are basically the same as the examples in the xray documentation found in here. Just some IDs were changed to match my project, same with the components and testPlanKey, and some unnecessary fields removed.
Anyone have any idea where the problem is and how to get the script to work?
Edit:
Got it to work by just simply removing the 'Content-Type': 'multipart/form-data' from header, it seems python requests fills that information itself if posting a dictionary of multiple files, and giving it multiple times breaks something. :)

 

2 answers

1 accepted

Got it to work by just simply removing the 'Content-Type': 'multipart/form-data' from the last post request header, it seems python requests fills that information itself if posting a dictionary of multiple files, and giving it multiple times breaks something. :)

Hi @mikael.huisko 

when you have mandatory fields, it is necessary to use the multipart endpoint.

With the multipart endpoint, you can add additional JSON files with the values for the mandatory fields, so they can be set up with the values from those files.

Please check the multipart endpoint and examples here: https://docs.getxray.app/display/XRAY/Import+Execution+Results+-+REST#ImportExecutionResultsREST-RobotFrameworkXMLresultsMultipart

Thank you.

Kind regards,
Rogério Paiva [Xray Support Team]

Hello @Rogério Paiva - Xray Xporter 

If you read my message you can see that I also linked the exact same documentation with examples on my message (except my link is for the xray cloud which my project is using and so the api url is different), so I have already gone through it. I'm using 2 json files, slightly modified from the example to fit my project, so I doubt that's the issue and maybe it's with the python script I posted on my message?

Hi @mikael.huisko 

Probably it is an issue in the python code, however, I am not a python expert so unfortunately, I cannot help you there.

What I would advise, is to first try the command line with your files:

curl -H "Content-Type: multipart/form-data" -X POST -F info=@issueFields.json -F results=@results.xml -F testInfo=@testIssueFields.json -H "Authorization: Bearer $token" https://xray.cloud.getxray.app/api/v2/import/execution/robot/multipart 

to ensure that your files are correct, and there isn't an issue with them.

After that, try to mimic the same request that the curl command sends using python code.

I hope this helps.

Thank you.

Kind regards,
Rogério Paiva [Xray Support Team]

I already figured out the issue and added the answer to this post earlier. Thank you anyway. :)

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Apps & Integrations

How we use Jira Service Management for our recruitment process! - Part 2

It is never about setting up a process and being done with it. Rather, the focus should always be on optimizing it for the best outcomes. Thus, we didn’t stop at setting up JSM for our recruitment pr...

194 views 0 5
Read article

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