Home > Back-end >  Import JSON in Python error BadStatusLine
Import JSON in Python error BadStatusLine

Time:04-11

I'm trying to import the Json data generated by an Impinj R420 reader.

The code i use is:

# import urllib library
import urllib.request
from urllib.request import urlopen

# import json
import json

# store the URL in url as
# parameter for urlopen
url = "http://10.234.92.19:14150"

# store the response of URL
response = urllib.request.urlopen(url)

# storing the JSON response
# from url in data
data_json = json.loads(response())

# print the json response
print(data_json)

When i execute the programm it gives the following error:

Traceback (most recent call last):
  File "C:\Users\V\PycharmProjects\Stapelcontrole\main.py", line 13, in <module>
    response = urllib.request.urlopen(url)
  File "C:\Users\V\AppData\Local\Programs\Python\Python310\lib\urllib\request.py", line 216, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Users\V\AppData\Local\Programs\Python\Python310\lib\urllib\request.py", line 519, in open
    response = self._open(req, data)
  File "C:\Users\V\AppData\Local\Programs\Python\Python310\lib\urllib\request.py", line 536, in _open
    result = self._call_chain(self.handle_open, protocol, protocol  
  File "C:\Users\V\AppData\Local\Programs\Python\Python310\lib\urllib\request.py", line 496, in _call_chain
    result = func(*args)
  File "C:\Users\V\AppData\Local\Programs\Python\Python310\lib\urllib\request.py", line 1377, in http_open
    return self.do_open(http.client.HTTPConnection, req)
  File "C:\Users\V\AppData\Local\Programs\Python\Python310\lib\urllib\request.py", line 1352, in do_open
    r = h.getresponse()
  File "C:\Users\V\AppData\Local\Programs\Python\Python310\lib\http\client.py", line 1374, in getresponse
    response.begin()
  File "C:\Users\V\AppData\Local\Programs\Python\Python310\lib\http\client.py", line 318, in begin
    version, status, reason = self._read_status()
  File "C:\Users\V\AppData\Local\Programs\Python\Python310\lib\http\client.py", line 300, in _read_status
    raise BadStatusLine(line)
http.client.BadStatusLine: {"epc":"3035307B2831B383E019E8EA","firstSeenTimestamp":"2022-04-11T11:24:23.592434Z","isHeartBeat":false}


Process finished with exit code 1

I know this is an error in the response where it gets a faulty HTTP status code. Yet i don't know how to fix the error.

Could you advice me how to fix this?

The {"epc":"3035307B2831B383E019E8EA","firstSeenTimestamp":"2022-04-11T11:24:23.592434Z","isHeartBeat":false} is an answer i expect.

Thanks in advance

CodePudding user response:

# store the response of URL
response = urllib.request.urlopen(url)

# storing the JSON response
# from url in data
data_json = json.loads(response())

Here you are actually calling response, I do not know what you want to achieve by that, but examples in urllib.request docs suggest that urllib.request.urlopen should be treated akin to local file handle, thus please replace above using

with urllib.request.urlopen(url) as f:
    data_json = json.load(f)

Observe that I used json.load not json.loads

CodePudding user response:

Even with

with urllib.request.urlopen(url) as f:
    data_json = json.load(f)`

i get the same BadStatusLine error.

I can't setup the reader any different, it can only sent a JSON response trough the IP-adress of the device. Is there a way to import the data without the HTTP Protocol?

  • Related