【发布时间】:2021-05-09 00:51:44
【问题描述】:
我正在尝试使用此链接中提供的 SNow API 将图像上传到 ServiceNow 事件:
https://developer.servicenow.com/dev.do#!/reference/api/orlando/rest/c_AttachmentAPI#r_AttachmentAPI-POSTmultipart
我在下面写了C# 代码来发布图片:
string url = "https://mycompany.service-now.com/api/now/attachment/upload/"
, userName = "MyUser", passwd = "MyPassword";
var httpClientHandler = new HttpClientHandler()
{
Credentials = new NetworkCredential(userName, passwd),
};
using (var httpClient = new HttpClient(httpClientHandler))
{
// Add an Accept header for JSON format.
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var multipartContent = new MultipartFormDataContent();
multipartContent.Headers.TryAddWithoutValidation("Content-Type", "multipart/form-data");
multipartContent.Add(new StringContent("incident"), "table_name");
multipartContent.Add(new StringContent("f264fd3a1bghghgjjhg8f7b4bcbb6"), "table_sys_id"); // id of my incident
multipartContent.Add(new ByteArrayContent(File.ReadAllBytes("D:\\MyImage.jpeg")), "uploadFile", "MyImage.jpeg");
var response = httpClient.PostAsync(new Uri(url), multipartContent).Result;
string result = response.Content.ReadAsStringAsync().Result;
MessageBox.Show(result);
}
但是,我不断得到:
请求的 URI 不代表任何资源
可能是什么问题以及如何相应地调整代码?
【问题讨论】:
-
只需从您的 url 中删除尾随“/” - string url = "mycompany.service-now.com/api/now/attachment/upload"
-
@Sergey 做到了,现在收到一条新消息:
Failed to create the attachment. File part might be missing in the request:( -
你必须发一条新消息。
-
你的意思是使用另一个事件?
-
对代码进行了一些更改,在标题中的键周围添加了双引号。我将发布新代码
标签: c# api httpclient servicenow servicenow-rest-api