Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: res:json() is nil #29

Open
whatwewant opened this issue Oct 3, 2019 · 7 comments · May be fixed by #30
Open

bug: res:json() is nil #29

whatwewant opened this issue Oct 3, 2019 · 7 comments · May be fixed by #30

Comments

@whatwewant
Copy link

whatwewant commented Oct 3, 2019

if content_type ~= "application/json"

and content_type ~= "application/json; charset=utf-8"

I found the bug is caused by the code, fixed ContentType values, it should using regular expression to detect the validity of ContentType

@tokers
Copy link
Owner

tokers commented Oct 4, 2019

@whatwewant Maybe I didn't catch your mean. Could you provide some cases to show me the problem? It seems good to me for the detection of Content-Type, is there any provision about the value of Content-Type?

@whatwewant
Copy link
Author

Some servers provide Content-Type: application/json;charset=utf-8, such as coding.net some apis, this will caused json() == nil

@whatwewant
Copy link
Author

and another, i think it better to validate two thing here

  1. json type
  2. charset

@tokers
Copy link
Owner

tokers commented Oct 9, 2019

@whatwewant
Actually I wanna keep it consistent with Python requests library. I have glanced requests' source code, it guessing the encoding way of content and decoding it. So maybe we can try to decode
the content too. Would you like to submit a PR to optimize this? Thanks!

@whatwewant
Copy link
Author

Well, I see. this issue is to solve json first. I also found pythons requests parses json at https://github.com/psf/requests/blob/eedd67462819f8dbf8c1c32e77f9070606605231/requests/models.py#L921 .
Only try to parse data ~~
That may be not better, but it is more compatible for something like this issue, because some old services is not reponse according standard.

@whatwewant
Copy link
Author

About encoding, I will try to make it solved like python.

@whatwewant
Copy link
Author

I also see json parse method do the same as python requests
in node-fetch(https://github.com/bitinn/node-fetch/blob/8c197f8982a238b3c345c64b17bfa92e16b4f7c4/src/body.js#L119) and axios(https://github.com/axios/axios/blob/1b07fb9365d38a1a8ce7427130bf9db8101daf09/lib/defaults.js#L61)
Also in go grequests, the json parse method is not decide by Content Type.
I agree you want to do better with response context-type indeed, but maybe the common solution is more compatible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants