why not use content-type in response header to auto set responseType in got? #1759
-
What would you like to discuss?responseType Note: When using streams, this option is ignored. The parsing method. Can be 'text', 'json' or 'buffer'. The promise also has .text(), .json() and .buffer() methods which return another Got promise for the parsed body. so,why not use content-type in response header to auto set responseType ? Checklist
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
auto-detecting
the current explicit approach puts you in control, not the server. you decide how to parse, regardles of what headers claim. |
Beta Was this translation helpful? Give feedback.
auto-detecting
responseTypefromContent-Typeheaders is a bad idea because:servers lie constantly - many APIs return JSON but send
text/plainor wrong content types. auto-parsing would break these.security risk - malicious servers could send harmful content-type headers to trigger unexpected parsing behavior.
unpredictability - explicit is better. if you expect JSON, saying
responseType: 'json'makes code clearer and avoids surprises.error handling mess - what if content-type says JSON but body isnt valid JSON? adds complexity and weird failure modes.
the current explicit approach puts you in control, not the server. you decide how to parse, regardles of what headers claim.