Mapping file extension to mime type in APIM pplicy?

Hi dudes and dudettes!

I'm fairly new to the azure platform but somehow found myself developing an API endpoint against a third-party system we can call "salesforce" .

Basically, I'm reading documents from salesforce into a legacy system that requires a response in the form

{ mimetype: application/pdf, base64encodedata : "xdsdfsadfasfsadfaf23423" } 

So far I've managed to combine two salesforce calls in one endpoint (getting the file info and the actual data is to separate calls, becuse reasons..?)

However, the mimetype is not stored in salesforce, only filename/file extension. So I need to map the ".pdf" into "application/pdf" (and so on)

Unfortunately, neither FileExtensionContentTypeProvider nor MimeMapping.GetMimeMapping are supported in apim policy (if I understand correct)

Any solutions?
Or I'm I forced to hardcode a mapping to the most common file types and just pray?

My outgoing policy is something like:
(where filename should really be mimetype)

 <set-body>@{ JObject inBody = context.Response.Body.As<JObject>(); string fileName = inBody["PathOnClient"].ToString(); string versionData = ((IResponse)context.Variables["VersionData"]).Body.As<string>(preserveContent: true); // string contentType; // new FileExtensionContentTypeProvider().TryGetContentType(fileName, out contentType); // contentType =contentType ?? "application/octet-stream"; // string mimeType = System.Web.MimeMapping.GetMimeMapping(fileName); return "{fileName:\""+fileName+" \" , base64encodedData:\""+System.Convert.ToBase64String(Encoding.UTF8.GetBytes(versionData)) + "\"}"; }</set-body> 
submitted by /u/pucko2000
[link] [comments]

Read more here: https://www.reddit.com/r/AZURE/comments/nwxfm1/mapping_file_extension_to_mime_type_in_apim_pplicy/

Content Attribution

This content was originally published by /u/pucko2000 at Microsoft Azure, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: