如何使用restsharp将表单数据发布到包含多个文件的rest api

时间:2019-02-12 16:04:40

标签: c# rest .net-core restsharp confluence-rest-api

我正在尝试将表单数据上传到此api https://developer.atlassian.com/cloud/confluence/rest 在左侧导航中查看内容->创建附件
它提供了这些参数,以便在我的页面上创建附件

-u admin:admin \
-X POST \
-H "X-Atlassian-Token: nocheck" \
-F "file=@example.txt" \
-F "minorEdit=true" \
-F "comment=Example attachment comment" \
http://myhost/rest/api/content/123/child/attachment

这是我的代码:

string apiUrl = "https://connectpoc.atlassian.net/wiki/rest/api/content/98336/child/attachment";

var client = new RestClient(apiUrl)
{
    Authenticator = new HttpBasicAuthenticator(
        "username",
        "password")
};

var request = new RestRequest(apiUrl, Method.POST);

request.AddHeader("X-Atlassian-Token", "nocheck");
request.AlwaysMultipartFormData = true;
request.AddHeader("Content-Type", "multipart/form-data");

request.AddParameter("minorEdit", "true", ParameterType.RequestBody);
request.AddParameter("comment", "Only for testing purpose", ParameterType.RequestBody);

//what should i do here to do add multiple files ??
request.AddParameter("file", "E:\\Data\\TestingPost.txt", ParameterType.RequestBody);

var response = client.Post(request); // i am getting bad request
var content = response.Content;

有人可以告诉我我在哪里犯错吗?
为什么我收到错误的请求
我是否正确传递了参数?
如果用户选择同时上传 image,doc,pdf 文件,我将如何处理多个上传?

0 个答案:

没有答案