I've created a rest-framework view to create an object.
Unfortunately, I get different responses if:
- I use the browsableAPI interface with raw data : 201 CREATED
- I use Hurl.it, (see parameters) : 201 CREATED
- I use curl (
curl -d @sample.json -H "Content-Type: application/json" -u admin -i "https://vincentle.pythonanywhere.com/samples/api/create/"
) : 302 FOUND- I use OkHttp/Retrofit in an Android app : 302 FOUND
Can you help me, at least in providing some debug strategies?
views:
class SampleCreateAPIView(ListCreateAPIView): queryset = Sample.objects.all() serializer_class = SampleSerializer permission_classes = (permissions.IsAuthenticated,) def perform_create(self, serializer): room = serializer.validated_data['room'] building = room.area.floor.building admins = building.customer_account.admin.all() if self.request.user in admins: super(SampleCreateAPIView, self).perform_create(serializer) else: raise PermissionDenied(detail=_('You are not in the administrators list for this room'))
serializers:
class SampleSerializer(serializers.ModelSerializer): room = serializers.PrimaryKeyRelatedField(many=False, queryset=Room.objects.all()) wifiaccesspoint_set = WifiAccessPointSerializer(many=True) class Meta: model = Sample fields = ('room', 'wifiaccesspoint_set',) def create(self, validated_data): wap_data = validated_data.pop('wifiaccesspoint_set') sample = Sample(room=validated_data['room']) sample.save() for wap in wap_data: WifiAccessPoint.objects.create(sample=sample, **wap) return sample
Edit 1 - Seems a curl problem This command:
curl -u admin -i "https://vincentle.pythonanywhere.com/fr/samples/api/create/" -H "Content-Type: application/json" -d @sample.json
is working fine and I get a 201 CREATED response.But this command:
curl -d @sample.json -H "Content-Type: application/json" -u admin -i "https://vincentle.pythonanywhere.com/samples/api/create/"
is giving me a 302 FOUND response.What is the difference between those 2 commands?
score:0
302 is a redirection. Since you're using authentication, double check where the redirection goes.
If it's to a login page, my feeling is you're not authenticated because you don't have the right auth scheme configured. curl -u
will probably send basic auth which you need to setup on your API (http://www.django-rest-framework.org/api-guide/authentication/#basicauthentication)
Credit To: stackoverflow.com
Related Query
- Different responses depending on request tool
- Django REST framework - views serving both regular webpages and API responses depending on the request
- How to set different permissions depending on the request method?
- How to show different inlines depending of current object field value
- Django REST framework - different views for same url depending on http method
- Use different serializer for request and reply
- different validation in drf serializer per request method
- How to use different Django Rest Framework serializers for the same request in Generic Views?
- Django tastypie: Resource show different in detailed request as in list request
- Django - referencing different settings files from wsgi depending on server
- Different 404 pages depending on the application in Django
- Defining different schema for both get and post request - AutoSchema Django Rest Framework
- How to set multiple permissions in one class view, depending on http request
- Different URL config depending on domain in Django
- How to use a different databases depending on login status in Django?
- How to create different objects depending on input?
- Django and post request to an external API in different views
- How to upload files in Django and save them in a different location depending on the format? (jpeg and doc)
- Django: How to re-route a request to a different view using middleware
- How can I use two Django authentication backends and give different access depending on which is used?
- Django DRF - Different Request and Response Serialization
- Return different reponse or data depending on method - Django rest framework
- DRF responses me by 403 error when I try to request as a client [Client Credential grant]
- Django-Rest-Framework: Can I create a viewset with two different methods with the same url_path, but different request methods?
- How to deserialize POST request using a different Serializer
- Django: How to retrieve in an AJAX POST request the value of two different variables?
- Different SearchQuerySet depending on the page
- Sending different errors depending on url Django
- What is the best way to get different output for different request methods in django REST API?
- Using the same block in a django template to display different information depending on a variable
More Query from same tag
- Django - Exception Value: Unexpected end of expression in if tag
- Backbone.js and Django Rest Framework registration and authentication
- Django Haystack: responsibilities of the template and model indexes
- urlpatterns = [...]+static(settings.STATIC_URL) get the escape symbol
- Model manager and model methods for a get query
- Django custom model Manager database error
- Create zip archive for instant download
- Unresponsive django-bootstrap3-datetimepicker (not showing calendar)
- django celery only calls 1 of 2 apply_async task
- python - can't install psycopg2 on centos 7
- How can I call a Python function from a form in Django?
- How to run the collectstatic script after deployment to amazon elastic beanstalk?
- Django using modals for deleting a user
- How to change the email verification link in allauth
- Can we include partial css files into a template file?