RawJSonForm.cs 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. // Contributed by Matt Senne from conjoinedcats.com
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. namespace BestHTTP.Forms
  7. {
  8. public sealed class RawJsonForm : HTTPFormBase
  9. {
  10. private byte[] CachedData;
  11. /// <summary>
  12. /// Prepares the request to sending a form. It should set only the headers.
  13. /// </summary>
  14. public override void PrepareRequest(HTTPRequest request)
  15. {
  16. request.SetHeader("Content-Type", "application/json");
  17. }
  18. /// <summary>
  19. /// Prepares and returns with the form's raw data.
  20. /// </summary>
  21. public override byte[] GetData()
  22. {
  23. if (CachedData != null && !IsChanged)
  24. return CachedData;
  25. Dictionary<string, string> dict = Fields.ToDictionary(x => x.Name, x => x.Text);
  26. string json = JSON.Json.Encode(dict);
  27. IsChanged = false;
  28. return CachedData = Encoding.UTF8.GetBytes(json);
  29. }
  30. }
  31. }