Fix invalidjson error for Wikitext: Get preview (and other commands)#134
Open
David-Gilman wants to merge 1 commit into
Open
Fix invalidjson error for Wikitext: Get preview (and other commands)#134David-Gilman wants to merge 1 commit into
invalidjson error for Wikitext: Get preview (and other commands)#134David-Gilman wants to merge 1 commit into
Conversation
`Wikitext: Get preview` throws `invalidjson: No valid JSON response` for any non-trivial wikitext document since v4.0.3. Root cause: `mwbot.request(args, options)` puts `args` into the POST `form` body. When `options.method` is overridden to `GET`, the underlying request library drops the form data; the actual HTTP request becomes `GET /w/api.php?format=json` with no `action` parameter. MediaWiki responds with the API help page as HTML, mwbot sees `typeof body !== 'object'`, throws invalidjson. `mwbot-patch.ts` correctly moves args to `qs` when method is `GET`, which works for small read-only queries (siteinfo, tokens, etc.). However, it cannot work for `action=parse` with a `text` parameter: the URL exceeds the server's URI length limit. Verified against en.wikipedia.org: - 4 KB wikitext via GET -> 200 application/json - 8 KB wikitext via GET -> 414 Request-URI Too Long (HTML) - 8 KB wikitext via POST -> 200 application/json Drop the GET overrides at all six call sites so mwbot's default POST is used. POST works for every MediaWiki API action regardless of payload size.
Owner
|
The purpose of introducing GET is to solve #125. I have indeed found that some sites will encounter exceptions when using POST. Perhaps in the worst case, we need to add a separate setting for this. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Wikitext: Get previewthrowsinvalidjson: No valid JSON responsefor any non-trivial wikitext document since v4.0.3. This PR fixes it by removing the{ method: 'GET' }override at all six call sites so mwbot's defaultPOSTis used.Reproduction
wikitext.host = "en.wikipedia.org")..wikitextfile (≥ ~8 KB).Wikitext: Get preview(Shift+Cmd/Ctrl+V).invalidjson: No valid JSON response.Root cause
mwbot.request(args, options)putsargsinto the POSTformbody. Whenoptions.methodis overridden to'GET', the underlying request library drops the form data —formis POST-only. The actual HTTP request becomesGET /w/api.php?format=jsonwith noactionparameter. MediaWiki responds with the API help page as HTML (Content-Type: text/html), mwbot then checkstypeof body !== 'object'and throwsinvalidjson: No valid JSON response.The
{ method: 'GET' }overrides were introduced in v4.0.3 (changelog: "Used explicit request methods to access the API to avoid some potential issues").Why
mwbot-patch.tsdoesn't save the previewsrc/mwbot-patch.tscorrectly intercepts GET-method calls and movesparamsintoqs. That works for small read-only queries —siteinfo,tokens,csrf, tag enumeration,readPagelookups. It cannot work foraction=parsewith atextparameter, because the request URL ends up containing the entire user document and exceeds the server's URI length limit.Verified against
en.wikipedia.org:The reproduction document I hit this with was 34 KB — far past the limit.
The fix
Drop the
{ method: 'GET' }overrides at all six call sites. mwbot's defaultPOSTworks for every MediaWiki API action regardless of payload size, and is what mwbot's defaults (form: {},qs: { format: 'json' },json: true) are designed around.Files touched:
src/export_command/wikimedia_function/view.ts—showViewer(preview / page view). This is the one that surfaces as the user-visible bug.src/export_command/wikimedia_function/bot.ts—compareVersion(siteinfo).src/export_command/wikimedia_function/page.ts—postPagecsrf/edit token fetches,pullPage,getValidTagList.The two
{ method: 'POST' }call sites are untouched.Possible follow-up (not in this PR)
With this change, no caller passes
method: 'GET'toMWBot.prototype.request. That meanssrc/mwbot-patch.tsbecomes dead code and its two imports inextension-{node,web}.tscan be removed. I left that out to keep the diff minimal; happy to add it (or do a separate PR) if you'd prefer.Test plan
,{method:"GET"}in the installed v4.0.4 bundle locally — preview works for a 34 KB document.curlagainsten.wikipedia.org(POST returns valid JSON for any size; GET 414s past ~8 KB).request(params: object, customRequestOptions?: RequestOptions)— second arg already optional).Environment encountered on
RoweWilsonFrederiskHolme.wikitext4.0.4en.wikipedia.org(default)