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

Headers missing from .send_get (python) using example


#1

Greetings. I was looking at 2.x python bindings for TestRail. Was attempting to just do the first send_get('get_case/1') example:

from testrail import *

client = APIClient(‘http://mine.testrail.net/’)
client.user = 'test@email.com’
client.password = ‘helloWorld’

case = client.send_get(‘get_case/1/1’)
print case

This returns:

testrail.APIError: TestRail API returned HTTP 400 ("Content-Type header missing (use Content-Type: application/json)")

I haven’t made any modifications to the code. Using the Python 2.x bindings ‘out of the box’. Any help is appreciated.


#2

Hey @joshtewing,

Try to change the client.send_get('get_case/1/1') to client.send_get('get_case/1')


#3

Thanks, sadly to no avail.

Full URL: http://mine.testrail.net/index.php?/api/v2/get_case/1

I also added the following to __send_request:

def __send_request(self, method, uri, data):
    url = self.__url + uri
    request = urllib2.Request(url)
    if method == 'POST':
        request.add_data(json.dumps(data))

    auth = base64.b64encode('%s:%s' % (self.user, self.password))
    request.add_header('Authorization', 'Basic %s' % auth)
    request.add_header('Content-Type', 'application/json')
    print request.has_header('application/json')
    print request.get_header('Content-Type')
    print request.get_full_url()
    print request.get_method()

has_header returns False
get_header returns None
get_full_url returns http://mine.testrail.net/index.php?/api/v2/get_case/1
get_method returns GET


#4

Just to double check you, in the admin page under site settings you have the API enabled and session authentication right?


#5

Yes, they are both enabled. :confused:

Thank you for your replies. If I can’t get it working in the next couple minutes I’m just going to rewrite the bindings and open a pull request.


#6

Hi Josh,

Thanks for your posting! The solution is simple and you would need to use HTTPS instead of HTTP:

client = APIClient('https://mine.testrail.net/')

Using just HTTP would result in a redirect which in turn results in a new HTTP request and different headers and this would explain why TestRail doesn’t see your Content-Type or Authorization headers.

I hope this helps!

Cheers,
Tobias


#7

Thanks, Tobias. I had the same problem, adding the “s” worked :slight_smile:


#8

Great to hear that, Sarah :slight_smile:

Cheers,
Tobias