Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Rest framework cannot delete using the DELETE method

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

How to solve the problem that rest framework cannot delete with the DELETE method? I believe that many inexperienced people are at a loss about this. Through this article, I hope you can solve this problem.

The framework used by the back-end API is rest framework, and the front end gets the list through GET, and there is no problem in adding data through POST.

However, an error is reported when you delete the operation through the DELETE method:

Strangely, the token of csrf already exists in cookie, but the DELET method just doesn't seem to recognize it, so the interface returns 403 directly.

Checked the information:

Because the POST request is to put the slave csrftoken in the post parameter, but the PUT/DELETE in django can only be checked for csrftoken by checking Header

So either the backend preprocesses after receiving the PUT/DELETE request, or the front end adds a Header in PUT/DELETE mode

For the time being, no useful solution has been found for the backend. It is very simple for the frontend to deal with this situation. You can add csrftoken to the request header in cookie:

Export async function removeExport (params) {return request ('/ api/collector/', {method: 'DELETE',body: {... params,method:' delete',}, headers: {"X-CSRFToken": Cookies.get ('csrftoken')}});}

In this way, the backend can normally get the csrftoken brought by the front end for authentication.

When using the PUT method for update operation, another problem was encountered. Instead of returning 403, it was changed to 405.

Error report is PUT Method Not Allow, the method is not allowed?

Check again where the cross-domain is handled:

Most of the methods are basically allowed, but I took a look at the stackoverflow and said that the two sides of the path of the request did not modify the id.

Originally requested url http://127.0.0.1:8080/api/promMonitor/

Modified the path http://127.0.0.1:8080/api/promMonitor/1/ of the frontend request below

Unexpectedly, it works, and then the front end modifies the request parameters: just bring the id to be modified to the path.

After reading this article, can you solve the problem that rest framework can't delete with DELETE method on your own? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel. Thank you for reading.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report