0

I am very new to python and robot framework and it would be great if someone could point out what is the mistake that I am doing. I am creating a simple function which will do a POST request to a test management tool called Zephyr and upload the Results. I have copied the code from 'Postman'(it works perfectly there) but doesn't seem to run from the Robot file. It is giving me a the error as:

{"errorCode":400,"message":"HTTP 400 Bad Request"}

My code:

import requests
def upload_to_zephyr():

    url = "https://api.zephyrscale.smartbear.com/v2/automations/executions/junit?projectKey=someKey"
    payload = {}
    files = [('file', ('junitresult.xml', open('/path-to-results/junitresult.xml', 'rb'), 'text/xml'))]
    headers = {
        'Content-Type': 'multipart/form-data',
        'Authorization': 'Bearer token value'
    }
    response = requests.request("POST", url, headers=headers, data=payload, files=files)
    print(response.text)

I have saved this in a file called uploadToZephyr.py. And then I have created a simple robot framework test to call this file. Both the python file and the robot file are in the same location.

*** Settings ***
Library  uploadToZephyr.py

*** Variables ***

*** Test Cases ***

Upload Results to Zephyr
    upload_to_zephyr

*** Keywords ***
Alapan Das
  • 17,144
  • 3
  • 29
  • 52
  • 1
    Not an answer, just a debugging hint - you can call `prepare()`, and check the `body` and `headers` attributes, to see what actually will be send, and how it differs from what the server is expecting. Just keep in mind they are in the Request class. – Todor Minakov Aug 18 '21 at 07:00
  • Thank you for the suggestion @TodorMinakov. I will keep this in mind for future definitely. – Alapan Das Aug 18 '21 at 09:03
  • Sure thing; I still don't know why it was not working in the first version, and works in the 2nd. – Todor Minakov Aug 18 '21 at 16:28

1 Answers1

0

I solved this by removing def upload_to_zephyr():. Now my uploadToZephyr.py looks like:

import requests

    url = "https://api.zephyrscale.smartbear.com/v2/automations/executions/junit?projectKey=someKey"
    payload = {}
    files = [('file', ('junitresult.xml', open('/path-to-results/junitresult.xml', 'rb'), 'text/xml'))]
    headers = {
        'Content-Type': 'multipart/form-data',
        'Authorization': 'Bearer token value'
    }
    response = requests.request("POST", url, headers=headers, data=payload, files=files)
    print(response.text)

And in my robot tests I have used:

*** Settings ***
Library  uploadToZephyr.py
Library  OperatingSystem

*** Variables ***

*** Test Cases ***
Upload Results to Zephyr
    ${output}=  Run And Return RC  python3 uploadToZephyr.py

*** Keywords ***
Alapan Das
  • 17,144
  • 3
  • 29
  • 52