我目前正在尝试调试本机包(react-native-uploader),该包用于尝试上传一捆文件(照片)。尽管可以在ios上运行,但当前实现仍为android返回以下错误:
Response{protocol=http/1.1, code=405, message=Method Not Allowed, url=${config.apiBase}/load/${this.props.id}/uploadconfirmation}
错误是由软件包中的这一行引起的:
Response response = client.newCall(request).execute();
客户在哪里
private final OkHttpClient client = new OkHttpClient()
请求在哪里
Request{method=POST, url=${config.apiBase}/load/${this.props.id}/uploadconfirmation, tag=null}
我已经使用formdata成功地向端点发布了帖子:
let tData = new FormData();
const that = this;
tData.append("confirmation_doc", {
uri: files[0].filepath,
type: "image/jpeg",
name: "confirmation_doc.jpg",
});
axios.post(
`${config.apiBase}/load/${this.props.id}/uploadconfirmation`,
tData
)
.then(response => {
Alert.alert(
"Success",
"Uploaded Successfully!",
[{ text: "OK", onPress: () => that.props.close() }],
{ cancelable: false }
);
});
我尝试遍历source code以确定哪些地方分崩离析,似乎一切都按预期方式发布(标题看起来不错,方法看起来不错,端点看起来不错)。我对Java不太熟悉,因此不胜感激。
答案 0 :(得分:0)
HTTP 405方法不允许...是客户端错误。
请求行中接收到的方法对于源服务器是已知的,但目标资源不支持。
如果JavaScript
有效,但是Java
无法...您可能正在寻找MultipartBuilder
...与MediaType.FORM
组合。
答案 1 :(得分:0)
为了解决此问题,我不得不放弃了我一直使用的react-native-uploader
软件包。以下是我如何解决此问题的方法:
let tData = new FormData();
this.state.selectedImages.forEach((item, i) => {
tData.append("doc[]", {
uri: item.uri,
type: "image/jpeg",
name: item.filename || `filename${i}.jpg`,
});
});
fetch(`${config.apiBase}/load/${this.props.id}/uploadconfirmation`, {
method: "post",
headers: {
Accept: "application/x-www-form-urlencoded",
Authorization: `Token ${this.props.token}`,
},
body: tData,
})
.then(res => res.json())
.then(res => {
Alert.alert(
"Success",
"Uploaded Successfully!",
[{ text: "OK", onPress: () => that.props.close() }],
{ cancelable: false }
);
})
.catch(err => {
console.error("error uploading images: ", err);
});