- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-01-2022 04:57 AM
Hi all,
I have a business requirement as follows.
We are working in ITAM, dealing with license details. So my client is using some licenses from Cisco.
As part of our SAM work, we have to download that Cisco license/devices information into ServiceNow by using API endpoints from the Cisco portal.
How can we achieve this?
Is there any solution documentation available, such as how we request API access and get a response from Cisco to download the file, and so on?
What is the flow for that needs step-wise documentation?
I did not have much experience with these APIs . Any leads would be helpful.
Thank you
Solved! Go to Solution.
- Labels:
-
Multiple Versions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-08-2022 09:56 PM
Now let’s dive in.
Return a File in ASP.NET Core Web API
In ASP.NET Core, a Web API action method usually returns an ActionResult object. When we want to return a file response, we can explicitly set the return type for the action method to be FileResult, which is a type inherited from ActionResult. But most times we still want to use the generic ActionResult because it covers many other useful return types, such as BadRequest, NoContent, and so on.
The FileResult type is an abstract type, and it has four concrete implementations: FileContentResult, FileStreamResult, VirtualFileResult, and PhysicalFileResult. These four variants don’t differ much, you can use any of them to construct a FileResult object at your convenience. In other words, no matter which of the four types you use, the client-side will not notice any difference when downloading a file from the API endpoint.
Apart from the constructors for the four concrete types, we can use a method File, provided natively from the ControllerBase class, to return a concrete FileResult object. The File method is very flexible and has a variety of overload methods to instantiate FileContentResult, FileStreamResult, VirtualFileResult, and PhysicalFileResult instances.
With the groundwork laid, we can easily write an action method as follows:
In the code above, we first find or generate the requested file from the local file system or a file storage, then read the file content. In the end (line 7), we return a FileContentResult object that is created using the method File(byte[] fileContents, string contentType, string fileDownloadName). Note that we should validate the request and user permissions before finding or generating the requested file. And you can choose to stream the file too.
In the File method (line 7), the second parameter contentType is used in the HTTP header to indicate the format of contents transmitted over the internet. In this demo, the action method returns a text file, so the content type is “text/plain”. You can set the value for contentType according to your file type. In the File method, the third parameter fileDownloadName determines the value for the filename attribute in the content-disposition HTTP response header. Browsers treat this value as the top priority to dictate the filename when downloading the file.
To sum up, the File method in line 7 sets the HTTP response headers as follows.
An example part of HTTP response headers for downloading a file
If the project includes Swagger support, then we can try out the API endpoint in the Swagger UI page. In the responses section, the Swagger UI provides a link for downloading the returned file.
An example Swagger UI page that demos a file downloading API endpoint
Determining content-type at Runtime
Sometimes, the file type should be determined at runtime. For example, some attachments are PDF files, some files are in CSV format, some are images, and so on. Then in this case, we should set the contentType based on the file extension. ASP.NET Core natively supports this kind of translation. An example implementation is as follows.
In lines 8 to 12, we utilize a FileExtensionContentTypeProvider to get contentType based on the file path. The FileExtensionContentTypeProvider is provided in the NuGet package Microsoft.AspNetCore.StaticFiles (refer to the using statement in line 1). This NuGet package is automatically included in ASP.NET Core Web projects, and it provides mappings between many commonly seen file extensions and content types. If the file extension is not in the mappings table, then you can add desired mappings in the following way.
If this utility is being used in many places, then you can extract it into a service class that focuses on content-type mapping.
Creating an Anchor Element for Downloading a File
Web users have become used to clicking a hyperlink to download a file. There are many ways to implement the hyperlink. For example, we can create a blob data URL, use a third-party JavaScript library, or serve files directly from a web server.
In this article, we are going to use the simplest way to create an anchor element without any JavaScript. The code is just one line, as shown in the following snippet.
The href attribute has a value that points to the API endpoint. Here the href value is api/students/files/1, which can be dynamically set in the front-end code according to the path and parameters of our API endpoint. Notice that the URL doesn’t have to include the filename or file extension.
Another attribute in the anchor element is the download attribute, which prompts the user to save the linked URL instead of navigating to it. You can read more about the download attribute here.
The rendered hyperlink works in all major browsers like Chrome, Edge, and Firefox. Users can either left-click a download link to download the file or right-click the link to choose “Save Link As” in the context menu and save the file. It works like the screen recording below.
screen recording for downloading a file from a hyperlink
Everything works as expected. Awesome!
This may help you,
Rachel Gomez
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-01-2022 05:01 AM
This may help
https://community.servicenow.com/community?id=community_question&sys_id=4d957b40db581c9cd82ffb243996194f
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-01-2022 05:23 AM
Hi,
you need to tell the 3rd party team to share the API details, sample request response body
Have a call with them and understand the details and based on that start your configuration.
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-01-2022 06:11 AM
Thank you, Ankur.
Yes, we have those API details, but I'm not sure how to start on this due to a lack of knowledge.
Is there any documentation available for better understanding?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-01-2022 09:37 PM
Hi,
It's the responsibility of 3rd party team to share the details around the API etc
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader