Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python requests library gives garbled response even though curl and browser give coherent text

Tags:

python

curl

api

Here is my problem:

1) Using a Browser, I can download a text file with this content:

URL: protocol://URL/otrs/nph-genericinterface.pl/Webservice/GenericTicketConnectorREST/Ticket/12621?UserLogin=P_6669&Password=XXXXXX

HTTP/1.1 200 OK Content-Type: application/json; charset=UTF-8 Content-Length: 669 Connection: close

{"ConfigItem":[{"CurInciState":"Operational","ConfigItemID":"245767","InciStateType":"operational","CurInciStateType":"operational","Number":"3266000189","CreateBy":"3","CurDeplState":"Production","LastVersionID":"463432","DeplState":"Production","CreateTime":"2017-01-18 10:09:01","DefinitionID":"466","VersionID":"463432","DeplStateType":"productive","CIXMLData":{"FilaResponsavel":"","type":"Active-active","tipo-inventario":"","VIPaddress":"sepap.camara.gov.br","id-inventario":"D0E7907294A0402C9AB0C1AD3D4C4704","Description":"Teste.","link-inventario":""},"Class":"Cluster Group","InciState":"Operational","CurDeplStateType":"productive","Name":"Cluster-Sepap"}]}

2) Using curl:

curl -k 'protocol://URL/otrs/nph-genericinterface.pl/Webservice/GenericTicketConnectorREST/Ticket/12621?UserLogin=P_6669&Password=XXXXXX'

{"Ticket":[{"EscalationTimeWorkingTime":"-382512","Age":846912,"PriorityID":"3","EscalationDestinationTime":"1483993892","ServiceID":"","EscalationDestinationIn":"106h 15m","SolutionTimeWorkingTime":"-357312","Type":"Unclassified","Responsible":"root@localhost","SolutionTimeDestinationDate":"2017-01-10 11:31:32","StateID":"4","ResponsibleID":"1","ChangeBy":"1","EscalationTime":-843312,"SolutionTimeDestinationTime":"1484055092","EscalationDestinationDate":"2017-01-09 18:31:32","OwnerID":"1","Changed":"2017-01-09 17:31:34","FirstResponseTimeWorkingTime":"-382512","RealTillTimeNotUsed":"0","GroupID":"1","Owner":"root@localhost","CustomerID":"CENTRO DE INFORMÁTICA","TypeID":"1","Created":"2017-01-09 17:31:32","Priority":"3 normal","FirstResponseTimeEscalation":1,"UntilTime":0,"EscalationUpdateTime":"0","QueueID":"1","Queue":"Postmaster","State":"open","Title":"Ticket#3","FirstResponseTimeDestinationDate":"2017-01-09 18:31:32","CreateBy":"1","TicketID":"12621","StateType":"open","UnlockTimeout":"0","EscalationResponseTime":"1483993892","FirstResponseTimeDestinationTime":"1483993892","EscalationSolutionTime":"1484055092","LockID":"1","ArchiveFlag":"n","TicketNumber":"30004295","SolutionTime":-782112,"CreateTimeUnix":"1483990292","Lock":"unlock","SolutionTimeEscalation":1,"SLAID":"","CustomerUserID":"P_7029","FirstResponseTime":-843312}]}

3) Using Python:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import requests
usuario = {'UserLogin':'P_6669','Password':'XXXXXX'}
headers = {'accept': 'application/json','content-type' : 'applicatio/json'}

ret = requests.get('protocol://URL/otrs/nph-genericinterface.pl/Webservice/GenericTicketConnectorREST/Ticket/12621', verify=False, params=usuario)

print "URL: " + ret.url
print "Status Code: " + str(ret.status_code)
print "Raw: " + str(ret.raw)
try:
        print "Text: " + str(ret.text)
except:
        print "TEXT: ERRO!"

print "Headers: " + str(ret.headers)
try:
        print "Json: " + ret.json()
except:
        print "Json: ERRO!"
print "Content: " + ret.content

Resultado:

URL: protocol://URL/otrs/nph-genericinterface.pl/Webservice/GenericTicketConnectorREST/Ticket/12621?UserLogin=P_6669&Password=XXXXXX

Status Code: 200

Raw: urllib3.response.HTTPResponse object at 0x7f8e86628510

TEXT: ERRO!

Headers: CaseInsensitiveDict({})

Json: ERRO!

Content: �����$��6�u��|A�^......... A lot of trash here ....

I already tried a lot of combinations with requests.get, requests.post, httplib2.Http. Always I got this error.

Why does this happen?

like image 650
Fabio Surrage Avatar asked Jan 19 '17 15:01

Fabio Surrage


1 Answers

Removing "br" from the Accept-Encoding of request headers, helped me to resolve this.

headers = {
   "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
   "Accept-Encoding": "gzip, deflate, br",
   "Accept-Language": "en-US,en;q=0.9",
   "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36",
}
like image 116
Krishna Sankara Avatar answered Sep 28 '22 05:09

Krishna Sankara