ChristianTremblay / pyhaystack

Pyhaystack is a module that allow python programs to connect to a haystack server project-haystack.org. Connection can be established with Niagara Platform running the nhaystack, Skyspark and Widesky. For this to work with Anaconda IPython Notebook in Windows, be sure to use "python setup.py install" using the Anaconda Command Prompt in Windows. If not, module will be installed for System path python but won't work in the environment of Anaconda IPython Notebook. You will need hszinc 1.3+ for this to work.
Apache License 2.0
74 stars 32 forks source link

function HayStackSession::_get requests zinc when told to get raw json #82

Closed criverc closed 5 years ago

criverc commented 5 years ago

If I do this:

from urrlib.parse import quote

session = SkysparkScramHaystackSession('http://address:port', username='someone', password=******, project='someproject', http_args={'tls_verify': False})

request = 'readAll(sometag)'
result = session._get_grid(f'eval?expr={quote(request)}',None,expect_format='json',raw_response=True)

Inspecting the request using a network sniffer I see:

GET /api/someproject/eval?expr=readAll%28sometag%29 HTTP/1.1
Host: address:port
User-Agent: python-requests/2.21.0
Accept-Encoding: gzip, deflate
Accept: */*
Connection: keep-alive
Authorization: BEARER authToken=sXtUkHhuwVUF0BIZ7cR6xH7tXYRNp7riWfqWSDAmbvGg-2a8
Cookie: skyarc-auth-9080=sXtUkHhuwVUF0BIZ7cR6xH7tXYRNp7riWfqWSDAmbvGg-2a8`

The Accept header is wrong it should be: Accept: application/json It follows that the server returns zinc and not json.

Without the raw_response argument the Accept header is correct. So it seems that the combination of raw_response=True and expect_format='json' does not work as expected.

sjlongland commented 5 years ago

Could you try pull request #83 and see if that resolves the issue for you?

criverc commented 5 years ago

I fetched #83 and merged into develop branch. I run the code snippet that I put in the description of this issue and now it works as expected. I get a binary json array in the body property of the response which I am able to parse using json.loads. Thanks!

sjlongland commented 5 years ago

No problems at all. :-)