如何在Unirest Java中解决multipart / form-data请求?

时间:2019-06-05 05:33:57

标签: java multipartform-data unirest

我正在尝试通过Unirest java和OpenProject API在OpenProject中创建附件。我收到警告

Jun 05, 2019 10:35:30 AM
org.apache.http.client.protocol.ResponseProcessCookies processCookies
WARNING: Invalid cookie header: "Set-Cookie: Expires=Wed, 12 Jun 2019 04:58:08 GMT; Path=/". Invalid 'expires' attribute: Wed, 12 Jun 2019 04:58:08 GMT
jsonResponse : 400

对于此代码,

HttpResponse<JsonNode> jsonResponse = Unirest.post(url).basicAuth("key","password")
         .header("Content-Type", "multipart/form-data")
         .header("accept", "application/json")
         .field("file", new File("abc.txt"))
         .asJson();
System.out.println("jsonResponse : "+jsonResponse.getStatus());
Unirest.shutdown();

在API中,要求该请求必须如下:

Headers
Content-Type: multipart/form-data
Body of request
--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--

所需的输出是:

响应码:200

(Response)Header
Content-Type: application/hal+json
(Response)Body
{
    "_type": "Attachment",
    "_links": {
        "self": {
            "href": "/api/v3/attachments/1"
        },
        "container" {
            "href": "/api/v3/work_packages/1"
        },
        "author": {
            "href": "/api/v3/users/1"
        },
        "staticDownloadLocation": {
            "href": "/api/v3/attachments/1/download"
        }
        "downloadLocation": {
            "href": "/some/remote/aws/url/image.png"
        }
    },
    "id": 1,
    "fileName": "cat.png",
    "filesize": 24,
    "description": {
        "format": "plain",
        "raw": "A picture of a cute cat",
        "html": "<p>A picture of a cute cat</p>"
    },
    "contentType": "image/png",
    "digest": {
        "algorithm": "md5",
        "64c26a8403cd796ea4cf913cda2ee4a9":
    },
    "createdAt": "2014-05-21T08:51:20Z"
}

(这是我的第一个问题,所以请告诉我我是否在格式化问题上有误)

谢谢!

0 个答案:

没有答案