Using cURL To Upload Files

Uploading files is fairly easy in SN but what if you need to upload a lot of files?

There are some solutions using the AttachmentCreator service which work great but I wondered if it was possible to do it without any programming at all. So I racked my brain then I remembered a tool: cURL.

cURL is a tool used for making all sorts of web related requests. The best part about is it a command line tool so if you are in need of automating web requests this is the tool to use. Theres several versions for every OS out there and its fairly well documented.

So lets take a look at how to use it…

If you haven’t already grab a copy of cURL from http://curl.haxx.se/

Logging in is a two step process, first we need to initialize our connection and set up a cookie:

The –cookie-jar cjar parameter creates a cookie called cjar so that we can save our information

-k tells cURL to just accept any certificates that the site offers, this gets us by HTTPS if that’s enabled on your instance.

-o output1.log tells curl to output to a log file instead of the screen

–location tells curl to follow any redirects

Finally we put our instance URL.

Once you run this command and you have a cookie jar created you can attempt to log in.

This command reuses some of the same parameters but also specifies all the data used to log in. Just replace USERNAME, PASSWORD and INSTANCE and you should get a positive response back.

The way to check that everything is working properly is in the output files. If they return the code that would be the homepage you are golden.

Finally once you have your cookie and are logged in you can start uploading files.

In this case I am uploading a PNG file called test that’s located in the same directory as cURL. If this is successful you will now be able to see the newly uploaded file on your instance.

Because this is an image file it will be available in the Image file browser but also at https://INSTANCE.service-now.com/scs/test.png or https://INSTANCE.service-now.com/test.pngx

Note: Theres also no need to keep logging in, once you are in you can continue to upload files.

If you’ve got tons of files no problem, just couple this technique with the for command (Windows Systems) and you can loop through all the files in a directory.

Heres a quick tutorial on that command: http://www.suite101.com/content/batch-file-programming-for-command-a37305

2 Replies to “Using cURL To Upload Files”

  1. Hi Snaug- I noticed old Posted Dates on your site. Is this site/are you defunct? I have some Service-Now Q’s that I’d like to share with you if you’re still around.

    Regards,
    Peter

Leave a Reply

Your email address will not be published. Required fields are marked *