从REST客户端查找swagger /文档POST的正确语法

问题描述 投票:0回答:1

我正在向/ document(swagger)发送帖子,该帖子应该使用正文内容上传文档

{
    "Application": "tickets",
    "File": "some binary data"
}

后面是使用招摇/文件,所以我相信我的标题没有正确。

问题是我没有得到需要发送的标头的正确组合:

Authorization : xxxx; 
Content-Type : multipart/form-data;
Content-Type : image/png;
Content-Type : application/json;
FileName : file_name

响应:

415 Unsupported Media Type

enter image description here

rest swagger document
1个回答
0
投票
            FileInfo fi = new FileInfo(@"file");
            byte[] fileContents = File.ReadAllBytes(fi.FullName);

            ByteArrayContent byteArrayContent = new ByteArrayContent(fileContents);
            byteArrayContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
            byteArrayContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
            {
                FileName = fi.Name, 
            };

            MultipartFormDataContent multiPartContent = new MultipartFormDataContent();
            multiPartContent.Add(new StringContent("document storage"), "Application");
            multiPartContent.Add(byteArrayContent, "File");

            string header = string.Format("WRAP access_token=\"{0}\"", "xxxx");

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "/document");
            request.Headers.Add("Authorization", header);
            request.Content = multiPartContent;

            HttpClient httpClient = new HttpClient();

            try
            {
                Task<HttpResponseMessage> httpRequest = httpClient.SendAsync(request, HttpCompletionOption.ResponseContentRead, CancellationToken.None);
                HttpResponseMessage httpResponse = httpRequest.Result;
                HttpStatusCode statusCode = httpResponse.StatusCode;
                HttpContent responseContent = httpResponse.Content;

                if (responseContent != null)
                {
                    Task<String> stringContentsTask = responseContent.ReadAsStringAsync();
                    String stringContents = stringContentsTask.Result;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
© www.soinside.com 2019 - 2024. All rights reserved.