简体   繁体   English

在react-native(iOS)上执行GET而不是POST的fetch()

[英]fetch() doing GET instead of POST on react-native (iOS)

I have the following code in my component: 我的组件中包含以下代码:

fetch('https://domain.com/api', {
  method: 'POST',
  headers: {'Accept': 'application/json', 'Content-Type': 'application/json'},
  body: JSON.stringify({
    key: 'value'
  })
}).
  then((response) => {
    console.log('Done', response);
  });

And every time the request is a GET (checked server logs). 并且每次请求都是GET(已检查的服务器日志)。 I thought it was something to do with CORS (but apparently no such thing in react-native ) and ATS (but already turned off by default, plus my domain is HTTPS). 我认为这与CORS (但在react-native显然没有此事)和ATS(但默认情况下已关闭,加上我的域是HTTPS)有关。 I've tried from a browser and from a curl and it worked perfectly, so a priori no issue on server configuration. 我已经从浏览器和curl尝试,并且效果很好,因此服务器配置没有任何先验问题。 Any idea what's going on here? 知道这里发生了什么吗?

I'm using the latest react-native version. 我正在使用最新react-native版本。

After further digging, it was definitely an issue with the API + fetch . 经过进一步的挖掘,这绝对是API + fetch I was missing a slash at the end of the URL and the API issued a 301, that fetch didn't handle correctly. URL末尾缺少斜线,API发出了301,表示fetch不正确。 So I don't know if there is something to fix in the fetch function (and underlying mechanisms) but this fixed my issue :) 所以我不知道fetch函数(和底层机制)是否有需要修复的东西,但这解决了我的问题:)

When a POST is redirected (in my case from http to https) it gets transformed into a GET. 当POST重定向时(以我的情况从http重定向到https),它将转换为GET。 (Don't know why...) (不知道为什么...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM