- I am sending a POST Request on one of my Azure Api Mangement APIs
In this post request there is a json body which contains base64 encoded data (zip file) like in this example:
{ "foo": "bar", "data": "your-base64-string" }
In the API Policy, I want to send a separate request to the Azure Storage REST API to save the above mentioned base64 string as a zip file.
Now the problem: I can successfully send a request to the Azure Storage REST API and download the resulting zip file via this statement:
<set-body>@{
var base64String = (string)context.Variables.GetValueOrDefault<JObject>("body")["data"]) ;
var bytes = Convert.FromBase64String(base64String);
var ascii = Encoding.ASCII.GetString(bytes);
return ascii;
}</set-body>
The thing is, I can download and open the zip file but I cannot extract the content of it as the archive got damaged.
Here is an example base64 encoded zip file string (ZIP File containing a text.txt):
UEsDBBQAAAAIAL1ZxE7MTbG/EwAAABMAAAAIAAAAdGVzdC50eHQLycgsVgCiRIWS1OKStMycVD0AUEsBAh8AFAAAAAgAvVnETsxNsb8TAAAAEwAAAAgAJAAAAAAAAAAgAAAAAAAAAHRlc3QudHh0CgAgAAAAAAABABgAI8Ac2LUa1QEjwBzYtRrVAZFuI861GtUBUEsFBgAAAAABAAEAWgAAADkAAAAAAA==
Here is my code for posting it to the Rest API:
<send-request mode="new" response-variable-name="putStorageRequest" timeout="5" ignore-error="true">
<set-url>@((string)context.Variables["blobUrl"])</set-url>
<set-method>PUT</set-method>
<set-header name="x-ms-date" exists-action="override">
<value>@((string) context.Variables["date"] )</value>
</set-header>
<set-header name="x-ms-version" exists-action="override">
<value>@((string) context.Variables["version"] )</value>
</set-header>
<set-header name="x-ms-blob-type" exists-action="override">
<value>BlockBlob</value>
</set-header>
<set-header name="Content-Type" exists-action="override">
<value>application/zip</value>
</set-header>
<set-header name="Authorization" exists-action="override">
<value>@{
string body = context.Request.Body.As<string>(preserveContent: true);
string contentType = "application/zip";
var base64String = (string)context.Variables.GetValueOrDefault<JObject>("body")["data"]) ;
var bytes = Convert.FromBase64String(base64String);
var ascii = Encoding.ASCII.GetString(bytes)
var contentLength = ascii.Length;
var hmacSha256 = new System.Security.Cryptography.HMACSHA256 { Key = Convert.FromBase64String(context.Variables.GetValueOrDefault<string>("storageKey")) };
var payLoad = string.Format("{0}\n\n\n{1}\n\n{2}\n\n\n\n\n\n\nx-ms-blob-type:BlockBlob\nx-ms-date:{3}\nx-ms-version:{4}\n{5}",
"PUT",
contentLength,
contentType,
context.Variables["date"],
context.Variables["version"],
"/" + context.Variables.GetValueOrDefault<string>("storageAccountName") + context.Variables.GetValueOrDefault<string>("resource"));
return "SharedKey "+ context.Variables.GetValueOrDefault<string>("storageAccountName") + ":" + Convert.ToBase64String(hmacSha256.ComputeHash(Encoding.UTF8.GetBytes(payLoad)));
}</value>
</set-header>
<set-body>@{
var base64String = (string)context.Variables.GetValueOrDefault<JObject>("body")["data"]) ;
var bytes = Convert.FromBase64String(base64String);
var ascii = Encoding.ASCII.GetString(bytes);
return ascii;
}</set-body>
</send-request>