React Native + fetch + API:DELETE 请求在 App 中失败,在 Postman 中有效 [英] React Native + fetch + API : DELETE request fails in App, works in Postman

查看:21
本文介绍了React Native + fetch + API:DELETE 请求在 App 中失败,在 Postman 中有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个通过 React Native 查询的 API.

We have an API that we are querying through React Native.

GET、POST 请求在我们的应用程序或 Postman 中都能完美运行.

GET, POST request work flawlessly either in our app or in Postman.

DELETE 不适用于该应用,但同样的请求适用于 Postman.

DELETE doesn't work on the App, but the same request works with Postman.

更奇怪的是:

如果我们使用 Django 或 Postman 删除资源,在手机上从我们的应用程序发出完全相同的请求将给我们一个 404 响应,告诉我们它确实被删除了.

If we delete the resource either with Django or Postman, making the exact same request from our app on the phone will give us a 404 response, telling us that indeed, it was deleted.

这意味着我们的应用程序的调用应该可以工作,因为我们从 API 获得了答案!

Which means that the call from our app is supposed to work since we get an answer from the API!

所以,基本上,DELETE 在应用程序中不起作用,但如果我们从其他地方删除资源,它的作用就好像它真的起作用一样.

我们用两部不同的手机对其进行了测试,以防万一...我们正在使用 Expo.

We tested it from two different phones, just in case... And we are using Expo.

我们的 DELETE 请求示例:

Example of our DELETE request :

https://apiurl/reservations/id_num {"method":"DELETE","headers":{"Accept":"application/json","Content-Type":"application/json","X-App-Token":"blablatoken","Authorization":"JWT blablasuperlongtoken"}}

https://apiurl/reservations/id_num {"method":"DELETE","headers":{"Accept":"application/json","Content-Type":"application/json","X-App-Token":"blablatoken","Authorization":"JWT blablasuperlongtoken"}}

更新:MattyK14 提出的问题很好,我们得到了 200 响应,这应该是我们的 GET 响应.我们在这条路径上进行了调查,但似乎找不到任何东西.

UPDATE: The question asked by MattyK14 was good, we get a 200 response which is supposed to be our GET response. We investigated on this path but can't seem to find anything.

我们正在使用 Axios 重建 API,因为我们找不到任何东西,我们会检查.

We are rebuilding the API using Axios as we can't find a thing and we will check.

推荐答案

发现错误!

我们需要在 API URL 的末尾添加一个斜杠.如果 DELETE 请求没有尾部斜杠,Django 会自动将它转换为 GET 请求.感谢您对 Matty 和 Arman 的评论.

We needed to add a trailing slash at the end of the API URL. Django automatically transforms your DELETE request into a GET request if it doesn't get a trailing slash. Thank you for your comments Matty and Arman.

这篇关于React Native + fetch + API:DELETE 请求在 App 中失败,在 Postman 中有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆