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

查看:151
本文介绍了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在该应用程序上不起作用,但邮递员也可以使用相同的请求.

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 {方法":删除",标题" :{"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的末尾添加一个斜杠. 如果Django的结尾没有斜杠,Django会自动将其DELETE请求转换为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天全站免登陆