Mihael Waschl
Mihael Waschl

Reputation: 350

After multiple requests send to the server 500 (Internal Server Error)

I am working on the Django application where I want to create multiple asynchronous requests with ajax to django server. It works fine if there is less than 5 asynchronous requests but if there is more requests django return 500 (Internal Server Error) for some of the requests. If I make synchronous ajax requests it works fine.

Code where I send multiple ajax requests:

for (i=2; i <= lastIndex; i++){
    pidForm['page_index'] = i;
    $.ajax({
        type: 'POST',
        url: '{% url "search_by_pid" %}',
        data: pidForm,
        success: function (data) {
            console.log(data);
            $(data.api_response.bugs).each(function(index, bugs){
                var id = bugs.id;
                createInnerHtml(id);
            });
        }
    })
}

My django view, where I send the ajax request:

def get_bug_by_pid(request):
    product_id_form = ProductIdForm()
    if request.method == 'GET':
        return render(request, 'search_bug_by_pid.html',
                      {'page_title': 'ProductID', 'product_id_form': product_id_form})

    elif request.method == 'POST':
        product_id = request.POST['product_id']
        if 'page_index' in request.POST:
            api_call = ApiCall()
            page_index = request.POST['page_index']
            api_response = api_call.bug_api_call_by_pid(product_id, page_index)
            return JsonResponse({'api_response': api_response,'product_id': product_id})
Internal Server Error: /pid/
Traceback (most recent call last):
  File "C:\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\base.py", line 126, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\base.py", line 124, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\decorators.py", line 21, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "C:\views.py", line 26, in get_bug_by_pid
    api_response = api_call.bug_api_call_by_pid(product_id, page_index)
  File "api_calls.py", line 33, in bug_api_call_by_pid
    return json.loads(r.content)
  File "C:\__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "C:\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Any idea how can I solve the problem or what I am doing wrong?

Upvotes: 0

Views: 2152

Answers (1)

Giannis Katsini
Giannis Katsini

Reputation: 1289

Your issue is not with your ajax request per-say. From the trace you provided, I can see that you are doing another API call to another service/url in your django view and your code is breaking there.

  File "api_calls.py", line 33, in bug_api_call_by_pid
    return json.loads(r.content)

By doing so many calls to the remote service, you may be either DDOSing that service, or the remote service is rate limiting you and not sending back valid JSON.

You can fix this fairly simply by either:

  1. Check the response from the service before json encoding it to ensure that you are receiving valid JSON.
    if response.status_code == 200:

    return response.json()

  1. Apply some of your own rate limiting in your service or set up channels(sockets) and stream the responses back asychronously, that way you can make each request synchronously.

I hope that this helps

Upvotes: 1

Related Questions