Search code examples
c#dotnet-httpclientservicenowservicenow-rest-api

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?


Solution

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