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

Django cannot process HTTP PUT/DELETE request

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

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.

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