Decoding content

The POST request will have some content with it. Decoding this data is simple; we just call the DecodeData() method on the request object. The Content-Type HTTP header determines which decoder will be used when decoding the payload. We will assume plain text will be sent (Content-Type equal to text/plain). This will be decoded into a normal string:

if (!req.HasData) 
   throw new BadRequestException(); 
                                  
string s = req.DecodeData() as string;
You can easily create your own content decoders. This is done by creating a class with a default constructor, implementing the Waher.Content.IContentDecoder interface, and making sure the assembly is initialized in the runtime inventory.

We then use the static Waher.Content.CommonTypes to help us with parsing the Boolean value. The TryParse method will accept 1, true, yes, or on as true values, and 0, false, no, and off as false values, using case insensitive comparison. If the content is not plain text, or not correctly formatted, we return a 400 Bad Request error back to the client:

if (s == null || !CommonTypes.TryParse(s, out bool OutputValue)) 
   throw new BadRequestException(); 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.95.38