使用Restify和猫鼬,我构建了一个基本的REST API。只要我以<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
<style> html { overflow-y: scroll; } </style>
<table class="table">
<thead>
<tr>
<th scope="col">Item</th>
<th scope="col" width="50%">Stock Quantity</th>
</tr>
</thead>
<tbody>
<tr class="categoryRow">
<td id="categoryTd" colspan="5">
<button class="btn" data-toggle="collapse" data-target=".collapseCategory" aria-expanded="true">category1
</button>
</td>
</tr>
<tr class="collapseCategory collapse show">
<td>Item1</td>
<td class="stockQuantity">3</td>
</tr>
<tr class="collapseCategory collapse show">
<td>Item2</td>
<td class="stockQuantity">3</td>
</tr>
<tr class="collapseCategory collapse show">
<td>Item3</td>
<td class="stockQuantity">3</td>
</tr>
</tbody>
</table>
作为内容发送JSON内容,一切就可以正常工作。
但是现在我需要将图像和内容一起发送,因此我切换到application/json
以使用特定的路由。而且似乎根本无法获得内容。
以下是失眠症发送给我的API的基本设置:
非常简单。目前,Mongodb模型仅需要multipart/form-data
和filename
(一旦工作,我将处理文件本身)。
这是我从服务器得到的响应:
url
所以它说没有任何字段内容。
时间轴显示字段已发送:
{
"code": "Internal",
"message": "Media validation failed: url: Path `url` is required., filename: Path `filename` is required."
}
这是我的职能
> POST /medias HTTP/1.1
> Host: localhost:3030
> User-Agent: insomnia/6.0.2
> Content-Type: multipart/form-data; boundary=X-INSOMNIA-BOUNDARY
> Accept: */*
> Content-Length: 221
| --X-INSOMNIA-BOUNDARY
| Content-Disposition: form-data; name="filename"
| test.jpg
| --X-INSOMNIA-BOUNDARY
| Content-Disposition: form-data; name="url"
| https://images.test.com/test.jpg
| --X-INSOMNIA-BOUNDARY--
* We are completely uploaded and fine
< HTTP/1.1 500 Internal Server Error
< Server: TestServer
< Content-Type: application/json
< Content-Length: 125
< Connection: Keep-Alive
< Content-MD5: KI89enLnYv9oMJxg5k4MXA==
< Date: Sun, 12 Aug 2018 22:50:31 GMT
< Request-Id: b20babc1-9f1b-466b-af7c-13d385cd8023
< Response-Time: 6
* Received 125 B chunk
* Connection #65 to host localhost left intact
server.post('/medias', (req, res, next) => {
if (!req.is('multipart/form-data')) {
return next(
new errors.InvalidContentError(`Expects 'multipart/form-data'`)
)
}
let data = req.body || {}
let media = new Media(data)
media.save((err) => {
if (err) {
return next(new errors.InternalError(err.message))
next()
}
res.send(201)
next()
})
})
上的console.log
显示为空。我已经对req.body
本身进行了控制台,以查看数据是否可以存储在其他任何地方,但找不到任何东西。
我在这里错过了什么?预先谢谢你。
答案 0 :(得分:1)
好吧,我终于有了base64编码。