Send attachment using REST api

Paramahanns
Tera Expert

Hello All,

Can you please help me understand how the requirement can be delivered..

I have seen many articles on posting an attachment to a specific incident/ change tickets. But take an example I want send a excel file to the below the api

https://<Servicenowurl>/api/now/attachment/file

then Service now imports it to the respective table.

Ex: Take a csv that has around 100 contacts and I post to a REST API link. Finally the excel has to be opened and loaded into the users table or a temporary table of service now.

If any body has done this please reply back on the steps and code.

Thanks.

Regards

Param

14 REPLIES 14

lks
ServiceNow Employee
ServiceNow Employee

Hello Param, I checked and you won't be able to use REST Attachment API for loading data into import set table as it is suppose to attach a file to a record as mentioned by Josh. Not really sure about your exact requirement but if it has to be done manually then I would say make it simple and load data manually using import sets. On the other hand if you want this import to be scheduled then please create a data source with an appropriate file retrieval method and a file path as well from system can retrieve the data and scheduled it accordingly. Doing so system will pick your attachment from the given file path accordingly and load the data. But, again you have to make sure that you have a respective import set table exist in the system for the same.



Please check Data sources and Schedule a data import links for more details.


Thanks, I am able to send file via REST API Explorer. But unable to send attachment via Postman to Service now,   can you provide the steps for Postman to service now.


I tried like below


URL: https://XXXX.service-now.com/api/now/attachment/upload?table_sys_id=XXXXXXXX057825740cf96190f&table_...



Method: Post


form-data :


file: <choose a file from upload button>


Header:


Content-type: multipart/form-data


Note: I used basic authentication



I got error as response:


{


      "error": {


              "detail": null,


              "message": "Failed to create the attachment. File part might be missing in the request."


      },


      "status": "failure"


}


Hello Prantish,



I am also getting the same error. Did you got any solution ?



Any help would be appreciated.



Thanks


josh_nerius
ServiceNow Employee
ServiceNow Employee

Hello,



The REST Attachment API is probably not what you want for the use case you describe. It can be used to attach a file to a record in ServiceNow, but it does not facilitate loading/transforming attached data.



Instead, take a look at this page on the docs site: Post CSV or Excel files directly to an import set. This allows you to upload an attachment to a special endpoint that will then load the attached data into an Import Set table and Transform it.



Before posting the file, you must first set up an Import Set and Transform map. Here's a post that explains the basics: Getting Started with Import Sets.



In summary, you will need to:



  1. Create an Import Set / Transform Map
  2. Use the special sys_import.do endpoint with the parameters described above to upload the file

bgworld
Giga Expert

I was able to send file using postman. Please find screenshot below:



find_real_file.png


find_real_file.png



Note from stackoverflow post:


Beware, when testing your webservice using chrome postman, you need to check the form data option(radio button) and File menu from the dropdown box to send attachment. Explicit provision of content-type as multipart/form-data throws an error. Because boundary is missing as it overrides the curl request of post man to server with content-type by appending the boundary which works fine.


Link: html - What is the boundary in multipart/form-data? - Stack Overflow



https://stackoverflow.com/questions/39280438/fetch-missing-boundary-in-multipart-form-data-post