- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-21-2019 02:43 PM
Hello SNOW Community,
I am trying to upload a document to an incident record using an ASP.Net C# application. Below is the code. I am receiving 400 Bad Request response. Any clues what I am doing wrong? Your help is much appreciated.
using (var httpClient = new HttpClient())
{
using (var request = new HttpRequestMessage(new HttpMethod("POST"), "https://myinstance.service-now.com/api/now/attachment/upload"))
{
request.Headers.TryAddWithoutValidation("Accept", "application/json");
string authData = string.Format("{0}:{1}", "username", "pwd");
string authHeaderValue = Convert.ToBase64String(Encoding.UTF8.GetBytes(authData));
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authHeaderValue);
var multipartContent = new MultipartFormDataContent();
multipartContent.Add(new StringContent("tableName"), "table_name");
multipartContent.Add(new StringContent("incidentSysid"), "table_sys_id");
multipartContent.Add(new ByteArrayContent(File.ReadAllBytes("filePath")), "uploadFile", Path.GetFileName("filePath"));
request.Content = multipartContent;
var response = await httpClient.SendAsync(request);
}
}
Thank you,
Naresh
Solved! Go to Solution.
- Labels:
-
Integrations
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-21-2019 04:09 PM
Hey Naresh,
I have done similar stuff in my local environment so I did modify and add the required code in my local visual studio. Basically I am against HTTPClient and would like to use RestSharp.dll to perform all these actions. This is a full-proof working solution . Also 400 Bad request can mean a lot of things and is basically a pain point to figure out what exactly is wrong in your situation. Try this code:
For Authorization it will have a value (same way as you are encrypting currently. make sure that it is a valid username/password in your system.I am sure you just wrote user password to avoid showing to us in the forum) like this in my Web.config:
Make sure that the path of the file which you provide is right. Try with a sample file in your desktop and then change it accordingly.
After you run this code, you will see a sample document attached :
Happy coding.
Please mark this as correct answer if this solves your problem.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-21-2019 04:09 PM
Hey Naresh,
I have done similar stuff in my local environment so I did modify and add the required code in my local visual studio. Basically I am against HTTPClient and would like to use RestSharp.dll to perform all these actions. This is a full-proof working solution . Also 400 Bad request can mean a lot of things and is basically a pain point to figure out what exactly is wrong in your situation. Try this code:
For Authorization it will have a value (same way as you are encrypting currently. make sure that it is a valid username/password in your system.I am sure you just wrote user password to avoid showing to us in the forum) like this in my Web.config:
Make sure that the path of the file which you provide is right. Try with a sample file in your desktop and then change it accordingly.
After you run this code, you will see a sample document attached :
Happy coding.
Please mark this as correct answer if this solves your problem.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-22-2019 08:47 AM
Hello Abhishek,
Thank you so much for the quick response. I was able to use your code and successfully upload attachments to an incident record.
Thanks again!
Naresh

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-22-2019 10:52 AM
Awesome buddy. Glad you were able to use my code.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-25-2025 05:43 AM
Hi Abhishek,
After 5 years of you writing the code, I've tried to reuse it but am ending up with the error - Bad request - {"error":{"message":"Missing parameter: table_name","detail":null},"status":"failure"} . Can you help me please?