在REST客户端中寻找正确的语法以进行大胆的/ document POST

时间:2018-08-30 15:36:28

标签: rest swagger document

我要发送邮件到/ document(swagger),后者应使用

的正文内容上传文档
f1 <- function(dat, rank = 1) {
        purrr::imap(dat, ~
                         dat %>%
                            count(!! rlang::sym(.y)) %>%
                            rename_all(~ c('variable', 'counts')) %>%
                            arrange(desc(counts)) %>%
                            slice(seq_len(rank))) #%>%
        #bind_cols - convert to a data.frame

}

f1(mtcars, 2)

后台使用的是swagger / document,所以我相信我的标题没有正确显示。

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

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

响应:

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

enter image description here

1 个答案:

答案 0 :(得分: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);
            }