Join 34,000+ subscribers and receive articles from our blog about software quality, testing, QA and security.
 

Using send_post in Python


#1

Hi everybody,

I try to send a request from python to testrail. In a simple case, that works fine.

But, I try to send a request created with my python code.

Example 1: This example do not work
My code is below:
d = {‘status_id’: 2, ‘comment’: ‘Updated automatically’, “custom_step_results”: [{“content”: “STEP 1”,“actual”: “step 1 OK blabla”,“status_id”: 1}]}
data=json.dumps(d)

result = client.send_post(
‘add_result/70684’,
data
)

I have testrail.APIError: TestRail API returned HTTP 400 (“Invalid or incomplete JSON string in API request.”)

Example 2: This example works fine:
result = client.send_post(
‘add_result/70684’,
{‘status_id’: 2, ‘comment’: ‘Updated automatically’, “custom_step_results”: [{“content”: “STEP 1”,“actual”: “step 1 OK blabla”,“status_id”: 1}]}
)

Can you help me ? Perhaps, a problem with JSON manipulation or encoding (UTF8).

Thanks for you help !

Regards,
Grégory


#2

I think, I don’t need data=json.dumps(d). But, I would like to create my request. And, I tried to work on string.

For example, I would like to replace STATUS by my value (2 for example). I have declared my string like that

d="{‘status_id’:STATUS, ‘comment’: ‘Updated automatically’}"
d= d.replace(STATUS, 2)
If I send this request, I have an error.

What are the good practices to use the POST in python ?

Thanks,
Greg


#3

Hi Greg,

Thanks for your posting. You don’t need to do the JSON encoding yourself and this is handled transparently by the binding. You can pass structured Python objects instead and you can find a send_post here:

http://docs.gurock.com/testrail-api2/bindings-python#examplepost_request

Cheers,
Tobias


#4

Hi Tobias,

Thank you for your quick response. It is ok for the JSON encoding.

But, can you explain me or give me some examples about structured Python objects ?

In fact, I would like to create my resquest to update each status of each steps. So, I need build this request from result.txt where each lines is a step:
Step 1 Passed: explanations
Step 2 Failed: explanations

Cheers,
Greg


#5

Hi Greg,

Sure, happy to help. Do you use the separated steps field for this in TestRail? This expects a list of steps, each with the steps content and optionally status, expected and actual result. The raw JSON format looks as follows:

http://docs.gurock.com/testrail-api2/reference-results#add_result_for_case

(see “Request Example”)

In Python, you would simply use a mix of Lists and Dictionaries to implement this and then pass this to send_post:

https://docs.python.org/2/tutorial/datastructures.html#more-on-lists
https://docs.python.org/2/tutorial/datastructures.html#dictionaries

Cheers,
Tobias


#6

Yes, I would like to use the steps content.

With your documentation about list and dictionaries, I will try to read and create my request.

Thanks a lot Tobias,
Greg


#7

Thanks, Greg, that sounds good. Just let me know in case any questions come up, happy to help :slight_smile:

Cheers,
Tobias