通过使用RestSharp C#发送正文和文件请求

时间:2019-06-25 07:33:26

标签: restsharp

我正在调用REST API,这需要在主体中发送所有4个参数-并选择“ form-data”。就像您使用邮递员一样。

PostMan工作完美,但是当我用C#编写代码时,我收到内部服务器错误-这意味着它无法识别请求。

参数: 类型 parent_id documentName 文件

公共字符串uploadDocument(字符串票证,字符串parent_ID,字符串documentName,字符串filePath,字符串endPoint)         {             字符串statusCode = string.Empty;

        var client = new RestClient(endPoint);
        var request = new RestRequest("/api/v2/nodes", Method.POST);

        request.RequestFormat = DataFormat.Json;
        request.AddHeader("Accept", "application/json");
        request.AddHeader("otcsticket", ticket);
        request.AddHeader("Content-Type", "application/json");
        //multipart/form-data
        request.AddJsonBody(new { type = "144", parent_id = parent_ID, name = documentName });
        request.AddFile(documentName, File.ReadAllBytes(filePath), Path.GetFileName(filePath), "application/octect-stream");

        var response = client.Execute(request);

        return "Success";
    }

0 个答案:

没有答案