Skip to content

Add User-Agent header and HTTP status validation to API requests#95

Open
ShuzoN wants to merge 1 commit into
masterfrom
claude/fix-api-parsing-Uko31
Open

Add User-Agent header and HTTP status validation to API requests#95
ShuzoN wants to merge 1 commit into
masterfrom
claude/fix-api-parsing-Uko31

Conversation

@ShuzoN
Copy link
Copy Markdown
Owner

@ShuzoN ShuzoN commented Feb 25, 2026

Summary

Enhanced the API request handling in zutool.ts by adding a User-Agent header to all HTTPS requests and implementing proper HTTP status code validation before parsing responses.

Key Changes

  • Added a User-Agent constant that mimics a Chrome browser to prevent potential request blocking by the API
  • Updated both fetch() and search() functions to include the User-Agent header in HTTPS requests
  • Implemented HTTP status code validation that rejects promises with a descriptive error message if the API returns a non-200 status code
  • Error messages now include both the status code and response body for better debugging

Implementation Details

  • The User-Agent string is defined as a module-level constant to avoid duplication across multiple request functions
  • Status code validation occurs in the res.on('end') callback before attempting to parse the JSON response, preventing parsing errors on error responses
  • Error handling maintains the existing promise-based API while providing more informative error messages

https://claude.ai/code/session_01AkaK8L4yHzPa2foSgBQY9s

…rror

zutool.jp started blocking requests without a browser-like User-Agent,
returning HTML instead of JSON (causing "Unexpected token < in JSON at
position 0"). Added a Chrome User-Agent header to both fetch() and
search() calls, mirroring the approach used by the eggplants/zutool
Python library. Also added HTTP status code checks to surface clearer
error messages when the API returns non-200 responses.

https://claude.ai/code/session_01AkaK8L4yHzPa2foSgBQY9s
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 this pull request may close these issues.

2 participants