How to remove `LOCATION` from the header options in DRF?
Asked Answered
S

2

2

I have a serializer which contains a URL field, By default, if there is a field named as URL then the value of this field is added in the HEADER option as LOCATION, I don't want to do this, and would like to remove the LOCATION option from the header.

This is my serializer:-

class DemoSerializer(serializers.ModelSerializer):
    class Meta:
        model = Demo
        fields = ('type', 'protocol', ---- 'url', 'somefield')

Now when the above serializer is used with POST/PUT/PATCH request to send data, I get this as response:- enter image description here

Is there any method, or way I can remove the LOCATION option from the header, without effecting my other serializers and view in the project??

Salome answered 9/8, 2021 at 4:52 Comment(3)
To clarify, this is the response in the screenshot, not the request, correct? Also, will you post it as text rather than a screenshot?Talion
Yes, you are right @Code-Apprentice, the screenshot is of response, with header options, but, if i post the text, when I do curl/http only the data is returned in response, so that's for ease I posted the screenshot.Salome
I don't understand what editing your question here with text has to do with curl.Talion
D
1

In View which is you want to remove Location header, you should override get_success_headers function.

Default

    def get_success_headers(self, data):
        try:
            return {'Location': str(data[api_settings.URL_FIELD_NAME])}
        except (TypeError, KeyError):
            return {}

Override

    def get_success_headers(self, data):
        return {}
Duly answered 9/8, 2021 at 6:8 Comment(0)
P
0

You could change or set the URL_FIELD_NAME to None which is used for setting the Location header:

REST_FRAMEWORK = {
    "URL_FIELD_NAME": None,  # This field is used in the Location header on a created resource
}
Pyrites answered 17/11, 2023 at 10:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.