https://github.com/cgqaq/chatgpt-sse-hide-example
ChatGPT web端看不到响应数据的原理及例子
https://github.com/cgqaq/chatgpt-sse-hide-example
api chatgpt chatgpt-web chatgpt3 under-the-hood
Last synced: about 1 year ago
JSON representation
ChatGPT web端看不到响应数据的原理及例子
- Host: GitHub
- URL: https://github.com/cgqaq/chatgpt-sse-hide-example
- Owner: CGQAQ
- Created: 2023-02-17T09:26:52.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-02-20T01:14:41.000Z (over 3 years ago)
- Last Synced: 2025-05-07T10:01:59.613Z (about 1 year ago)
- Topics: api, chatgpt, chatgpt-web, chatgpt3, under-the-hood
- Language: HTML
- Homepage:
- Size: 13.7 KB
- Stars: 29
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# chatgpt网页版看不到响应数据的原理及例子
如图:


是收到了响应数据的,有5.8 KB,但是 EventStream 却什么都没有。
一开始确实毫无头绪,但 [@phodal][phodal] 给出了思路,觉得是一些 http 的奇技淫巧,后来我顺着这个思路找下去发现不可能是 http 层面的问题,因为 [spec][spec] 说明如果不是 event、data、id 或 retry 就会被浏览器丢掉,js 没有机会做骚操作,我又翻了下 [webkit 的源码][webkit eventsource] 佐证了这一事实:浏览器并没有特殊实现,完全按照 spec 来的

最后经过研究发现其实服务端并没有进行什么骚操作,只是浏览器端使用了[server-sent-event(sse)][sse], 但是没有用标准的 EventSource,而是自己fetch,只不过 `request header` 加上了 ['Accept': 'text/event-stream'][fetch]. 具体操作可以看本仓库示例
[phodal]: https://github.com/phodal
[webkit eventsource]: https://github.com/WebKit/webkit/blob/main/Source/WebCore/page/EventSource.cpp#L371-L388
[sse]: https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events
[fetch]: https://github.com/CGQAQ/chatgpt-sse-hide-example/blob/b5f673ad57a0b987291f9eca4ef35e67e1b6f479/index.html#L11
[spec]: https://html.spec.whatwg.org/multipage/server-sent-events.html#event-stream-interpretation