我正在尝试使用RestSharp上传json文件,但由于未上传Json文件而收到错误。
我正在使用的摘录:
var fullFilePath= @"C:\test\jsontest.json";
var client = new RestClient("https://testapi/url");
var request = new RestRequest(Method.PUT);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("Authorization", "Bearer "+ Mytoken);
request.AddHeader("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
request.AddQueryParameter("metadata", MyMetadata);
request.AddQueryParameter("schemaReference", MySchemaRef);
request.AddFile("file", fullFilePath,"application/json");--->Not able to upload Not sure
request.AlwaysMultipartFormData = true;
IRestResponse response = client.Execute(request);
string test = response.Content.ToString();
我还尝试过使用字节进行转换,即
request.AddFile("file",bytes,fullFilePath,"application/json")
但是我总是收到错误消息
仅应使用结构化文件(JSON)上传/更新
代码:DBK_430
string jsonread = String.Empty;
using (StreamReader r = new StreamReader(fullFilePath))
{
jsonread = r.ReadToEnd();
}
var data = jsonread;
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(data);
对于我来说,发送API请求非常困难,因为我是第一次使用 PUT 操作上传文件。
我也检查了这些博客
但没有解决
我认为上载Json文件时缺少某些内容。
我已经使用邮递员进行了测试,在那里我可以收到API json响应。当我删除表单数据中的文件上传并进行测试时,它抛出了与上述相同的错误,它应仅使用结构化文件(JSON)上传/更新”。 因此,它不是通过c#上传Json文件。