Ir para o conteúdo

Exemplo de Script - Converter Valor Codificado em Base64 em Texto

Este plug-in de resposta REST foi projetado para converter um valor codificado em Base64 em texto. No exemplo específico em que esse script foi usado, o valor codificado era XML de codificação Base64 em uma resposta REST JSON.

#r "Newtonsoft.Json.dll"
using System;
using System.Text;
using System.Net.Http;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

string columnToDecode = "file_base64";

string originalContent = await Response.Content.ReadAsStringAsync();
var jobject = JObject.Parse(originalContent);

// Decode the field in the json
byte[] data = Convert.FromBase64String(jobject[columnToDecode].ToString());
string decodedString = Encoding.UTF8.GetString(data);

// Replace the data in json with the decoded value
jobject[columnToDecode] = decodedString;

// Update the response
string newJson = jobject.ToString();
Response.Content = new StringContent(newJson, Encoding.UTF8, "application/json");

Limitações

Não se espera que este plug-in funcione em codificação binária.