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

mikael.huisko September 10, 2021

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

0 votes
Answer accepted
mikael.huisko September 27, 2021

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. :)

0 votes
Rogério Paiva - Xray Xporter
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.
September 22, 2021

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]

mikael.huisko September 26, 2021

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?

Rogério Paiva - Xray Xporter
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.
October 14, 2021

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]

mikael.huisko October 15, 2021

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
AUG Leaders

Atlassian Community Events