// Contributed by Matt Senne from conjoinedcats.com
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BestHTTP.Forms
{
public sealed class RawJsonForm : HTTPFormBase
{
private byte[] CachedData;
///
/// Prepares the request to sending a form. It should set only the headers.
///
public override void PrepareRequest(HTTPRequest request)
{
request.SetHeader("Content-Type", "application/json");
}
///
/// Prepares and returns with the form's raw data.
///
public override byte[] GetData()
{
if (CachedData != null && !IsChanged)
return CachedData;
Dictionary dict = Fields.ToDictionary(x => x.Name, x => x.Text);
string json = JSON.Json.Encode(dict);
IsChanged = false;
return CachedData = Encoding.UTF8.GetBytes(json);
}
}
}