Reputation: 127
I am newbie to python. I am trying to extract data from API's. I tried extracting data in my local using postman and it extracts the data. But when I use python requests I am getting connection aborted error. Can someone please help me in understanding this issue.
Below are the code that I have tried:
import requests
from requests import request
url = "https://abcd/smart_general_codes?category=BANK"
payload={}
headers = {
'TenantId': 'IN0XXX',
'Accept-Language': 'en_us',
'Transfer-Encoding': 'chunked',
'fileType': 'json',
'Authorization': 'Basic XXXXXXXXXX'
}
response = requests.get(url, headers=headers, data=payload, verify=False)
print(response.status_code)
print(response.text)
Code2:
import http.client
conn = http.client.HTTPSConnection("main.com")
payload = ''
headers = {
'powerpayTenantId': 'IN0XXX',
'Accept-Language': 'en_us',
'Transfer-Encoding': 'chunked',
'fileType': 'json',
'Authorization': 'Basic XXXXXXXXXX'
}
conn.request("GET", "abcd/smart_general_codes?category=BANK", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
Both using httpclient and requests method throws the below error:
urllib3.exceptions.ProtocolError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response',))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "login_2.py", line 20, in <module>
response = requests.get(url, headers=headers, data=payload, verify=False)
File "/usr/lib/python3/dist-packages/requests/api.py", line 72, in get
return request('get', url, params=params, **kwargs)
File "/usr/lib/python3/dist-packages/requests/api.py", line 58, in request
return session.request(method=method, url=url, **kwargs)
File "/usr/lib/python3/dist-packages/requests/sessions.py", line 520, in request
resp = self.send(prep, **send_kwargs)
File "/usr/lib/python3/dist-packages/requests/sessions.py", line 630, in send
r = adapter.send(request, **kwargs)
File "/usr/lib/python3/dist-packages/requests/adapters.py", line 490, in send
raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response',))
Upvotes: 0
Views: 5816
Reputation: 127
I have solved the issue. In postman the accept language was showing as en_us but updating that to en_US worked.
Upvotes: 1
Reputation: 1
Try to add a fake user-agent (such as chrome) and your cookies (if needed) in headers like this:
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36',
'cookie': '<Your cookie>'
}
By the way, you can get your cookies by typing document.cookie
in your browser's developer console.
Upvotes: 0