Как решить multipart / form-data запрос в unirest Java? - PullRequest
0 голосов
/ 05 июня 2019

Я пытаюсь создать вложение в OpenProject через Unirest Java и OpenProject API.Я получаю предупреждение

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"
}

(Это мой первый вопрос, поэтому скажите мне, если ясделать ошибку при форматировании вопроса)

Спасибо !!

...