dijs / wiki

Wikipedia Interface for Node.js
MIT License
315 stars 61 forks source link

URL query parameter incompatibility - "origin=*" #55

Closed triestpa closed 6 years ago

triestpa commented 6 years ago

Hello, First off, thanks for the great library.

The Issue -

At the moment, the default URL query string is not compatible with all MediaWiki APIs. The origin=* parameter causes issues on some MediaWikis. For instance - Running

wiki({ apiUrl: 'http://awoiaf.westeros.org/api.php' }).search('Winterfell').then(console.log)

results in this URL request -

https://awoiaf.westeros.org/api.php?list=search&srsearch=Winterfell&srlimit=50&format=json&action=query&origin=*&redirects=

Which returns an error - 'origin' parameter does not match Origin header.

Solution

Removing the origin=* parameter results in a correct API response -

https://awoiaf.westeros.org/api.php?list=search&srsearch=Winterfell&srlimit=50&format=json&action=query&redirects=
{
  "query": {
    "searchinfo": {
      "totalhits": 28
    },
    "search": [
      {
        "ns": 0,
        "title": "Winterfell",
        "snippet": "| name\t\t= <span class='searchmatch'>Winterfell</span>\n| image\t\t= [[File:Ted Nasmith A Song of Ice and Fire <span class='searchmatch'>Winterfell</span>.jpg|350px]]\n",
        "size": 28785,
        "wordcount": 4547,
        "timestamp": "2017-04-11T20:21:56Z"
      },
      {
        "ns": 0,
        "title": "Crypts beneath Winterfell",
        "snippet": "#REDIRECT [[crypt of <span class='searchmatch'>Winterfell</span>]]\n",
        "size": 55,
        "wordcount": 6,
        "timestamp": "2016-06-21T17:50:29Z"
      },
     ...
    ]
  }
}

Proposed Change

It seems that a reasonably simple solution to this would be allowing a configuration option to in the wiki() object constructor, such as -

wiki({ 
    apiUrl: 'http://awoiaf.westeros.org/api.php',
    setOriginParam: false
})

What do you think?

dijs commented 6 years ago

Thanks for the great issue description and potential solution!

Let me give this a try.

dijs commented 6 years ago

Fixed in v4.1.0

Also added to documentation.

triestpa commented 6 years ago

Great, thanks for the fix!