I have this API endpoint which signs up a user and is used by mobile apps:
/mobile/v1/signup
I recently migrated the app from Django 1.7 to Django 1.10. After this I started to get this strange error. If this API accessed the second time, it is returning 412 (precondition failed)
error.
I searched the net and as long as I understand, the following happens:
The first time, the backend returns Etag
in the response. Then, this Etag
is then sent back to the server the second time this API is accessed. If this does not match, it returns 412
.
I solved this by manually setting a random value to the Etag header of the response of this API:
response['Etag'] = datetime.now().timestamp()
return response
But I don't think this is a good solution.
What is the proper way of solving this?
My middleware:
MIDDLEWARE = [
'corsheaders.middleware.CorsMiddleware',
'msd.middleware.RequestIdMiddleware',
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'apps.dashboard.middleware.DashboardMiddleware',
'api.middleware.UserAgentMiddleware',
]
UserAgentMiddleware:
class UserAgentMiddleware(MiddlewareMixin):
# A middleware that adds a "user_agent" object to request
def process_request(self, request):
request.user_agent = SimpleLazyObject(lambda: get_user_agent(request))