Skip to Content

Scripting Example - Convert Binary to Response to Text

This Plugin script example was used with a Microsoft Azure REST API, and works with a raw Binary response. The Plugin essentially converts the API endpoint response into something that Vinyl can consume/read.

For example, if the REST API endpoint returns something like:

"success"

Then the Plugin converts the response to something that Vinyl can read, like:

{
"Message": "success"
}

Script Example

#r "Newtonsoft.Json.dll"
using System;
using System.Net.Http;
using Newtonsoft.Json;
// Read the response content
var messageBytes = await Response.Content.ReadAsByteArrayAsync();
var message = Convert.ToBase64String(messageBytes);
// Wrap the message in a json formatted text
message = JsonConvert.SerializeObject(new { Message = message });
// Replace the response content with the formatted json
Response.Content = new StringContent(message, null, "application/json");