Saltar al contenido

Ejemplo de Script: Convertir un Valor Codificado en Base64 a Texto

Este complemento de respuesta REST está diseñado para convertir un valor codificado en Base64 en texto. En el ejemplo específico en el que se utilizó este secuencia de comandos, el valor codificado era XML codificado en Base64 en una respuesta JSON REST.

#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");

Limitaciones

No se espera que este complemento funcione con codificación binaria.