我正在尝试将文件(图像)从我的react原生应用程序上传到后端服务器。RESTFUL风格的后端服务器通过特定url上的PUT方法接受文件发送。我被react-native卡住了,试图找到通过PUT方法发送文件的正确方式。
我正在尝试复制
curl -X PUT -T "/path/to/file" "http://myputserver.com/puturl.tmp"
我发现在浏览器上做这件事的XMLHttpRequest method,但在react-native上不能工作。有没有人经历过这种情况,请帮帮忙!
发布于 2019-08-13 17:08:47
fetch('https://mywebsite.com/endpoint/', {
method: 'PUT',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
firstParam: 'yourValue',
secondParam: 'yourOtherValue',
}),
})
.then((response) => response.json())
.then((responseJson) => {
alert(responseJson)
})
.catch((error) => {
console.error(error)
})
reference
发布于 2016-04-03 17:35:00
获取
使用React Native支持的fetch api。下面是official documentation的一个示例。
根据specs的说法,Fetch支持PUT
。
fetch('https://mywebsite.com/endpoint/', {
method: 'PUT',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
firstParam: 'yourValue',
secondParam: 'yourOtherValue',
})
})
发布于 2017-02-27 23:27:33
你可以像上面的答案一样使用PUT。您可能缺少“Content-Type”:“multipart/form-data;”。
const config = {
method: 'PUT',
headers: {
'Accept': 'application/json',
'Content-Type': 'multipart/form-data;',
'Authorization': 'Bearer ' + 'SECRET_OAUTH2_TOKEN_IF_AUTH',
},
body: data,
}
在这篇博客文章中有更多信息:http://snowball.digital/Blog/Uploading-Images-in-React-Native
https://stackoverflow.com/questions/36380676
复制相似问题