file-upload - 向 Redmine API 发送 POST 请求时如何修复“422 Unprocessable Entity”?
问题描述
我正在尝试使用 redmine rest api 创建一个 wiki 页面。身份验证成功,但由于 422 错误,未创建 wiki 页面。
Redmine 文档说:“当尝试创建或更新具有无效或缺少属性参数的对象时,您将收到 422 Unprocessable Entity 响应。这意味着无法创建或更新该对象。”
但我似乎可以找出我搞砸的地方。当我提出第二个请求 - “提出请求”时,问题就出现了。
所以我们知道问题出在那个部分的某个地方。
我的猜测是,它要么是文件路径,要么是内容类型。
这是我到目前为止所拥有的......
const wordDocument="C:\Users\adasani\Desktop\practice\RedmineApi/RedmineText.txt";
creatingWikiPage_Request(wordDocument);
function creatingWikiPage_Request(wordDocument) {
axios({
method: 'post',
url: '<redmine_url>/uploads.json',
headers: { 'Content-Type': 'application/octet-stream' },
params: { 'key': '<api-key>' },
data: wordDocument
})
.then(function (response) {
console.log("succeeed---> ");
console.log(response.data.upload.token)
axios({
method: 'put',
url: '<redmine_url>/projects/Testing/wiki/WikiTesting.json',
headers: { 'Content-Type': 'application/octet-stream' },
params: { 'key': '<api-key>' },
data: {
"wiki_page": {
"text": "This is a wiki page with images, and other files.",
"uploads":[
{ "token": response.data.upload.token, "filename": "RedmineText.txt", "content-type": "text/plain" }
]
}
}
})
.then(response => {
console.log("PUT is Succeed-->>>")
console.log(response)
})
.catch(error => {
console.log("Error-->>")
console.log(error.response)
})
})
.catch(function (error) {
console.log("failed-----> ");
console.log(error.response.statusText, "-->", error.response.status);
console.log(error.response.headers)
console.log(error.message)
console.log("failed-----> ");
})
}
我想在我的 redmine 仪表板中看到一个 wiki 页面正在创建,但我收到 422 错误。
解决方案
您正在将更新请求发送到 JSON api,即<redmine_url>/projects/Testing/wiki/WikiTesting.json
使用Content-Type: application/octet-stream
. 因此,Redmine 无法解析 PUTed 有效负载,因为它不知道数据的格式。
为了解决这个问题,您应该始终确保在发布数据时设置正确的内容类型。在这种情况下,您应该在将任何 JSON 格式的数据发送到 Redmine 时将Content-Type
标头设置为。application/json
请注意,原则上,您可以将 XML 数据发送到 Redmine 并返回 JSON。输出格式由以 URL(.json
或.xml
)结尾的文件决定,您发送的数据格式始终由Content-Type
标头标识。
推荐阅读
- postgresql - PostgreSQL JDBC 驱动程序从日志记录中隐藏准备好的语句参数
- azure - 使用默认 Databricks 文件系统或默认 DBFS 或 DBFS 根的哪种数据加密?
- javascript - 无法将文件对象从 reactjs 发送到 nodejs
- node.js - 在 node.js 中使用来自控制器的 fastify-redis
- oracle - 在 Oracle 12c 中创建具有分区年和子分区月的表
- android - 最适合创建闹钟应用程序的语言/框架?
- swift - 在切换到 UITabbarController 中的不同选项卡之前关闭当前选项卡上的推送视图控制器
- sap-cloud-platform - 错误 - 无法为本地连接添加“SAP-Connectivity-Authentication”标头
- maven - maven传递依赖错误版本
- javascript - 使用来自多个 Fetch API 请求的变量