CouchDB 如何使用浏览器获取API发出多部分/相关请求?

nsc4cvqm  于 2022-12-09  发布在  CouchDB
关注(0)|答案(1)|浏览(113)

我的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--
62o28rlo

62o28rlo1#

似乎xhr、fetch和event axios(实际上它可以使用不同的技术)都不支持multipart/related
我没有找到任何直接的规范描述,但是,在这里你可以找到一些相关的问题(Question 1Question2)和researches
因此,您似乎应该手动编译您的请求。Here您可以找到有关如何执行此操作的一些说明。

相关问题