R: Mediawiki-api Digest, Vol 129, Issue 2

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

R: Mediawiki-api Digest, Vol 129, Issue 2

viviana paga
Thanks so much Brian for your detailed answer, thanks to you I'm starting to understand something !

Viviana


Da: Mediawiki-api <[hidden email]> per conto di [hidden email] <[hidden email]>
Inviato: domenica 29 aprile 2018 14:00
A: [hidden email]
Oggetto: Mediawiki-api Digest, Vol 129, Issue 2
 
Send Mediawiki-api mailing list submissions to
        [hidden email]

To subscribe or unsubscribe via the World Wide Web, visit
        https://lists.wikimedia.org/mailman/listinfo/mediawiki-api
or, via email, send a message with subject or body 'help' to
        [hidden email]

You can reach the person managing the list at
        [hidden email]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Mediawiki-api digest..."


Today's Topics:

   1. Wikimedia API redirect 301 (viviana paga)
   2. Re: Wikimedia API redirect 301 (bawolff)


----------------------------------------------------------------------

Message: 1
Date: Sat, 28 Apr 2018 14:33:29 +0000
From: viviana paga <[hidden email]>
To: "[hidden email]"
        <[hidden email]>
Subject: [Mediawiki-api] Wikimedia API redirect 301
Message-ID:
        <[hidden email]>
       
Content-Type: text/plain; charset="windows-1252"

Hi everyone,

I'm developing an Ajax webservice that query Wikimedia Api Service, but I get a very strange behavior; in fact my query sometimes works perfectly and sometimes doesn't work at all (301 internal redirect) .

This is the response by the server:    X-Cors-Redirect-1: 301 https://commons.wikimedia.…metadata&sroffset=0&callback=?

Could you help me to understand why or what I miss in my code?

This is my code :

$.ajaxPrefilter(function (options) {
            if (options.crossDomain && jQuery.support.cors)     {
                const https = (window.location.protocol === 'http:' ? 'http:' : 'https:');
                options.url = https + '//cors-anywhere.herokuapp.com/' + options.url;
            }
            if ( !options.beforeSend) {
                options.beforeSend = function (xhr) {
                    xhr.setRequestHeader('Api-User-Agent', 'OpenArtImages/Beta (http://localhost:8080; [hidden email]; Wikipedia User: Vivsss)');
                    xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
                    xhr.setRequestHeader('Origin', 'http://localhost:8080');
                    xhr.setRequestHeader('Strict-Transport-Security', 'max-age=106384710; includeSubDomains; preload');

                    xhr.withCredentials = true;
                }
            }
        });

firstRequest = $.get( '<a href="https://commons.wikipedia.org/w/api.php?origin=*&amp;action=query&amp;list=search&amp;format=json&amp;srsearch=File:'&#43;inputWord&#43;incategory&#43;'&#43;fileh:">https://commons.wikipedia.org/w/api.php?origin=*&action=query&list=search&format=json&srsearch=File:'+inputWord+incategory+'+fileh:>600& &prop=imageinfo|pageids|titles&srnamespace=6&rawcontinue=&srinfo=totalhits|suggestion&srlimit='+limit+'&iiprop=timestamp|user|url|size|sha1|mime|metadata'+offset+'&callback=?',
        function (response) {
                ///// ........ I get Data ......... /////
}


Thank you so much,
Viviana Paga
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.wikimedia.org/pipermail/mediawiki-api/attachments/20180428/2f89c4d1/attachment-0001.html>

------------------------------

Message: 2
Date: Sat, 28 Apr 2018 16:48:41 +0000
From: bawolff <bawolff+[hidden email]>
To: "MediaWiki API announcements & discussion"
        <[hidden email]>
Subject: Re: [Mediawiki-api] Wikimedia API redirect 301
Message-ID:
        <CA+oo+[hidden email]>
Content-Type: text/plain; charset="UTF-8"

That header comes from https://cors-anywhere.herokuapp.com/ not
MediaWiki. However, its usually set if mediawiki redirects you, which
might happen if you are not using https.

However, https://cors-anywhere.herokuapp.com/ is not needed. &origin=*
will tell MediaWiki to send appropriate CORS * headers.

On top of that, you don't need CORS headers at all when using
&format=json&callback=foo as you could directly inject into a <script>
tag. Given you're not doing that, you probably don't want to set
callback= as that messes up JSON parsers.

Additionally, you are setting the incorrect content-type header
(You're doing a GET, so there should not be a content-type header),
the strict-transport-security header doesn't make sense for a client,
and if your in a browser, you can't override the origin header (if
you're not in a webbrowser, you may be able to override it, but there
should be no need to).

So in conclusion, you probably want to get rid of all ajaxPrefilter
stuff except the part setting Api-User-agent. You also probably want
to get rid of &callback= in the url, so you can properly parse the
returned data using JSON.parse

--
Brian

On Sat, Apr 28, 2018 at 2:33 PM, viviana paga <[hidden email]> wrote:
> Hi everyone,
>
> I'm developing an Ajax webservice that query Wikimedia Api Service, but I
> get a very strange behavior; in fact my query sometimes works perfectly and
> sometimes doesn't work at all (301 internal redirect) .
>
> This is the response by the server:    X-Cors-Redirect-1: 301
> https://commons.wikimedia.…metadata&sroffset=0&callback=?
>
> Could you help me to understand why or what I miss in my code?
>
> This is my code :
>
> $.ajaxPrefilter(function (options) {
>             if (options.crossDomain && jQuery.support.cors)     {
>                 const https = (window.location.protocol === 'http:' ?
> 'http:' : 'https:');
>                 options.url = https + '//cors-anywhere.herokuapp.com/' +
> options.url;
>             }
>             if ( !options.beforeSend) {
>                 options.beforeSend = function (xhr) {
>                     xhr.setRequestHeader('Api-User-Agent',
> 'OpenArtImages/Beta (http://localhost:8080; [hidden email];
> Wikipedia User: Vivsss)');
>                     xhr.setRequestHeader('Content-Type', 'application/json;
> charset=UTF-8');
>                     xhr.setRequestHeader('Origin', 'http://localhost:8080');
>                     xhr.setRequestHeader('Strict-Transport-Security',
> 'max-age=106384710; includeSubDomains; preload');
>
>                     xhr.withCredentials = true;
>                 }
>             }
>         });
>
> firstRequest = $.get(
> '<a href="https://commons.wikipedia.org/w/api.php?origin=*&amp;action=query&amp;list=search&amp;format=json&amp;srsearch=File:'&#43;inputWord&#43;incategory&#43;'&#43;fileh:">https://commons.wikipedia.org/w/api.php?origin=*&action=query&list=search&format=json&srsearch=File:'+inputWord+incategory+'+fileh:>600&
> &prop=imageinfo|pageids|titles&srnamespace=6&rawcontinue=&srinfo=totalhits|suggestion&srlimit='+limit+'&iiprop=timestamp|user|url|size|sha1|mime|metadata'+offset+'&callback=?',
>         function (response) {
>                 ///// ........ I get Data ......... /////
> }
>
>
> Thank you so much,
> Viviana Paga
>
> _______________________________________________
> Mediawiki-api mailing list
> [hidden email]
> https://lists.wikimedia.org/mailman/listinfo/mediawiki-api
>



------------------------------

Subject: Digest Footer

_______________________________________________
Mediawiki-api mailing list
[hidden email]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-api


------------------------------

End of Mediawiki-api Digest, Vol 129, Issue 2
*********************************************

_______________________________________________
Mediawiki-api mailing list
[hidden email]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-api