首页 > 解决方案 > 如何使用浏览器获取 api 放置多部分/相关请求?

问题描述

我的 api 服务器接受multipart/related各种 POST 请求。但是我找不到任何资源来解释如何使用浏览器获取 api 来放置一个这样的请求。几乎所有链接都告诉如何使用FormData,但表单数据的内容类型不是multipart/related,我寻求。我需要上传一个 JSON 以及各种附件文件。如何发出这样的获取请求?

有没有标准的方法来做到这一点,比如FormData对象?还是我必须手动编写?

这就是我期望的输出

PUT /target/SpaghettiWithMeatballs?new_edits=false HTTP/1.1
Accept: application/json
Content-Length: 1030
Content-Type: multipart/related; boundary="864d690aeb91f25d469dec6851fb57f2"
Host: localhost:5984
User-Agent: CouchDB

--2fa48cba80d0cdba7829931fe8acce9d
Content-Type: application/json

{
    "_attachments": {
        "recipe.txt": {
            "content_type": "text/plain",
            "digest": "md5-R5CrCb6fX10Y46AqtNn0oQ==",
            "follows": true,
            "length": 87,
            "revpos": 7
        }
    },
    "_id": "SpaghettiWithMeatballs",
    "_rev": "7-474f12eb068c717243487a9505f6123b",
    "_revisions": {
        "ids": [
            "474f12eb068c717243487a9505f6123b",
            "5949cfcd437e3ee22d2d98a26d1a83bf",
            "00ecbbc54e2a171156ec345b77dfdf59",
            "fc997b62794a6268f2636a4a176efcd6",
            "3552c87351aadc1e4bea2461a1e8113a",
            "404838bc2862ce76c6ebed046f9eb542",
            "5defd9d813628cea6e98196eb0ee8594"
        ],
        "start": 7
    },
    "description": "An Italian-American delicious dish",
    "ingredients": [
        "spaghetti",
        "tomato sauce",
        "meatballs",
        "love"
    ],
    "name": "Spaghetti with meatballs"
}
--2fa48cba80d0cdba7829931fe8acce9d
Content-Disposition: attachment; filename="recipe.txt"
Content-Type: text/plain
Content-Length: 87

1. Cook spaghetti
2. Cook meetballs
3. Mix them
4. Add tomato sauce
5. ...
6. PROFIT!

--2fa48cba80d0cdba7829931fe8acce9d--

标签: javascriptbrowsercouchdb

解决方案


似乎 xhr、fetch 和 event axios(实际上它可以在底层使用不同的技术)都不支持multipart/related开箱即用。

但是,我没有找到任何直接的规范描述,在这里您可以找到一些相关的问题(问题 1问题2 )和研究

因此,您似乎应该手动编译您的请求。在这里您可以找到有关如何执行此操作的一些说明。


推荐阅读