Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
473 views
in Technique[技术] by (71.8m points)

Uploading image to ServiceNow in C# via REST API

I am trying to upload image to ServiceNow incident using SNow API provided in this link: https://developer.servicenow.com/dev.do#!/reference/api/orlando/rest/c_AttachmentAPI#r_AttachmentAPI-POSTmultipart

I've written below C# code to post the image:

    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);
    }

However, I keep getting:

Requested URI does not represent any resource

What could be the problem and how to adjust the code accordingly?

question from:https://stackoverflow.com/questions/66050411/uploading-image-to-servicenow-in-c-sharp-via-rest-api

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

After removing the "/" at the end of the url, and also adding double qoutes around the keys in the headers as I read from this post:

https://community.servicenow.com/community?id=community_question&sys_id=328614ffdb00eb808e7c2926ca9619ad&view_source=searchResult

The final working code is:

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 fileStream = new ByteArrayContent(File.ReadAllBytes("D:\Image.jpeg"));
            fileStream.Headers.Remove("Content-Type");
            fileStream.Headers.Add("Content-Type", "application/octet-stream");
            fileStream.Headers.Add("Content-Transfer-Encoding", "binary");
            fileStream.Headers.Add("Content-Disposition", $"form-data;name="uploadFile"; filename="{"Image.jpeg"}"");

            var multipartContent = new MultipartFormDataContent();
            multipartContent.Add(new StringContent("incident"), ""table_name"");
            multipartContent.Add(new StringContent("f264fd3a1bghghgjjhg8f7b4bcbb6"), ""table_sys_id"");
            multipartContent.Add(fileStream, "uploadFile");

    var response = httpClient.PostAsync(new Uri(url), multipartContent).Result;
    string result = response.Content.ReadAsStringAsync().Result;
    MessageBox.Show(result);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...