1

我希望你能帮助我解决我目前的问题。我的任务是将 webrequest (POST) 发送到 API。有问题的 API 来自 Openproject

这是我们的想法:

  • 用户通过 Sharepoint-Webpage 向 Openproject 发送一个想法,将自动创建一个工作包(有效)

  • OpenProject-Response 被记录,工作包的 ID 被提取(有效)

  • 现在我们有了 ID,如果用户提供了附件,则可以向该 ID 发送附件。(这不起作用)

由于 API 需要表单数据/多部分,我们尝试了这样的表单(Javascript):

var form = new FormData();

form.append("Content-Disposition: form-data; name='metadata' Content-Type: application/json; charset=UTF-8", "{ 'fileName': 'cute-cat.png', 'description': { 'raw': 'A cute kitty, cuddling with its friends!'} })");

form.append("'file'; 'filename'='attachment'Content-Type: image/png", <filedata>);

这是我们能想出的最好主意(我没有类似的经验)

当我们通过浏览器(Firefox 或 Chrome)发送请求时,我们得到错误

“400 - 错误请求”。瓮:openproject-org:api:v3:errors:InvalidRequestBody"

我们通过浏览器发送的请求内容如下所示:

   -----------------------------264232436135129208361200980561

   Content-Disposition: form-data; name="Content-Disposition: form-data; name='metadata' Content-Type: application/json; charset=UTF-8"



{ 'fileName': 'cute-cat.png', 'description': { 'raw': 'A cute kitty, cuddling with its friends!'} }

   -----------------------------264232436135129208361200980561

   Content-Disposition: form-data; name="Content-Disposition: form-data; name='file'; 'filename'='attachment'Content-Type: image/png"

  <data>
  
  -----------------------------264232436135129208361200980561--

请注意,浏览器会自动附加“Content-Disposition: form-data; name="-part, 即使我们提供了它。如果我们在发送的表单数据变量中不包含该部分,我们仍然会得到相同的错误,浏览器会将变量的多个值解释为一个长字符串。

如果我完全使用上面提到的代码,它可以通过 Postman 完美运行。但是,一旦我将这些行放入 Sharepoint-Page 并执行脚本,它就会显示错误。

你们有什么想法吗,如何正确创建这个表单数据/多部分对象(或者它的名称是什么?Openproject的官方文档看起来像这样:

To add an attachment to a work package, a client needs to issue a request of type multipart/form-data with exactly two parts.

The first part must be called metadata. Its content type is expected to be application/json, the body must be a single JSON object, containing at least the fileName and optionally the attachments description.

The second part must be called file, its content type should match the mime type of the file. The body must be the raw content of the file. Note that a filename must be indicated in the Content-Disposition of this part, however it will be ignored. Instead the fileName inside the JSON of the metadata part will be used.

    Content-Type: multipart/form-data

    Body

    --boundary-delimiter
    Content-Disposition: form-data; name="metadata"
    Content-Type: application/json; charset=UTF-8

    {
      "fileName": "cute-cat.png",
      "description": {
        "raw": "A cute kitty, cuddling with its friends!"
      }
    }

    --boundary-delimiter
    Content-Disposition: form-data; name="file"; filename="attachment"
    Content-Type: image/png

    PNG file data
    --boundary-delimiter--

对不起,如果这些问题很愚蠢,但我不知道更好。任何帮助表示赞赏。

4

0 回答 0