API request sent twice

761 views
Skip to first unread message

Alex Arshavski

unread,
May 20, 2014, 3:43:53 AM5/20/14
to django-res...@googlegroups.com
Hi,

I have a django app with the rest framework.
I have the following settings:

REST_FRAMEWORK = {
   ...
    'DEFAULT_RENDERER_CLASSES': (
        'rest_framework.renderers.UnicodeJSONRenderer',
        'rest_framework.renderers.BrowsableAPIRenderer',
    ),
   ...
}

For some reason when I call any API via the browser, I see 2 same requests in my log:

[20/May/2014 07:19:30] "GET /api/v1/ HTTP/1.1" 200 8899
[20/May/2014 07:19:30] "GET /api/v1/ HTTP/1.1" 200 1625

I get only one request if I call the same api via curl or via ajax call from the browser console.
I also get only one request if I type in the browser any other app url except apis.
If I call any API with "?format=json", I get only one request.
What I noticed is that on the first call I go through BrowsableAPIRenderer and UnicodeJSONRenderer and on the second call I go only through UnicodeJSONRenderer.
You can also see that the response sizes are different.
Do you have any idea why every API get called twice?

Thanks, Alex A.

Reinout van Rees

unread,
May 22, 2014, 3:18:33 PM5/22/14
to django-res...@googlegroups.com
On 20-05-14 09:43, Alex Arshavski wrote:
> For some reason when I call any API via the browser, I see 2 same
> requests in my log:
>
> [20/May/2014 07:19:30] "GET /api/v1/ HTTP/1.1" 200 8899
> [20/May/2014 07:19:30] "GET /api/v1/ HTTP/1.1" 200 1625
>
> I get only one request if I call the same api via curl or via ajax call
> from the browser console.
> I also get only one request if I type in the browser any other app url
> except apis.
> If I call any API with "?format=json", I get only one request.
> What I noticed is that on the first call I go through
> BrowsableAPIRenderer and UnicodeJSONRenderer and on the second call I go
> only through UnicodeJSONRenderer.
> You can also see that the response sizes are different.
> Do you have any idea why every API get called twice?

Well, in the end it is the *browser* that calls the API twice.
Django isn't forcing it to do that.
The only way django could force the API to be called twice is by sending
a "304 redirect", but what you see is that it is a regular "200 OK", so
that's not the problem.

=> There's something in your javascript, I presume.



Reinout

--
Reinout van Rees http://reinout.vanrees.org/
rei...@vanrees.org http://www.nelen-schuurmans.nl/
"If you're not sure what to do, make something. -- Paul Graham"

Reply all
Reply to author
Forward
0 new messages