I need help populating the Body Parameters for the documentation of this endpoint. It shows as empty:
I realize that the reason this is happening is because I am currently reading the request body and then converting the information to PackageUpdate. Since I am reading the raw request body this way, the task has no parameters which will cause the help page to not specify any.
Sample Request:
{
"PackageId": "package/3e585e1c-d6cd-4b6c-aa1c-aa674d11c944",
"SoldDateTime": "2018-08-13 19:57:54.000",
"GuaranteeTermExpiresDate": null
}
PackageUpdate class:
[DataContract]
public class PackageUpdate: BaseRequest
{
/// <summary>
/// Get or Set the package ID of the package to update
/// </summary>
[Required]
[DataMember]
public string PackageId { get; set; }
/// <summary>
/// Get or Set the Sold Date Time field of the package
/// </summary>
[DataMember]
public DateTime? SoldDateTime { get; set; }
/// <summary>
/// Get or Set the Guarantee Term Expires Date field of the package
/// </summary>
[DataMember]
public DateTime? GuaranteeTermExpiresDate { get; set; }
/// <summary>
/// Get or Set the Premium Promised On field of the package
/// </summary>
[DataMember]
public DateTime? PremiumPromisedOn { get; set; }
}
UpdatePackageAsync Method:
/// <summary>
/// Updates a package.
/// </summary>
[Route("update"), Description("Patch a specific package")]
[HttpPatch]
[ResponseType(typeof(Package))]
public async Task<IHttpActionResult> UpdatePackageAsync()
{
string requestBody = await Request.Content.ReadAsStringAsync();
PackageUpdate request = new PackageUpdate();
try
{
JsonSerializerSettings settings = new JsonSerializerSettings{ MissingMemberHandling = MissingMemberHandling.Error };
request = JsonConvert.DeserializeObject<PackageUpdate>(requestBody, settings);
}
catch(Exception e)
{
return BadRequest("Patch Error -> " + e.Message);
}
//Do stuff with request
}
How can I possibly get the Body Parameters field on the documentation to reflect the properties of PackageUpdate, and still have access to the raw request body?
I previously tried the below solution from a different post but it wouldn't allow me access to the request body as a string.
public async Task<IHttpActionResult> UpdatePackageAsync([FromBody] PackageUpdate request)
I ended up trying multiple solutions that I found on the site and then found a solution (here) that allows me to seek to the beginning of the stream, copy to a new stream, and return the body to my Task.
Solution:
using (var stream = new MemoryStream())
{
var context = (HttpContextBase)Request.Properties["MS_HttpContext"];
context.Request.InputStream.Seek(0, SeekOrigin.Begin);
context.Request.InputStream.CopyTo(stream);
string requestBody = Encoding.UTF8.GetString(stream.ToArray());
}
Applicable to my code, I used it in this way:
public async Task<IHttpActionResult> UpdatePackageAsync(PackageUpdate request)
{
string requestBody = ReadInputStream();
// do stuff with request
}
private string ReadInputStream()
{
using (var stream = new MemoryStream())
{
var context = (HttpContextBase)Request.Properties["MS_HttpContext"];
context.Request.InputStream.Seek(0, SeekOrigin.Begin);
context.Request.InputStream.CopyTo(stream);
return Encoding.UTF8.GetString(stream.ToArray());
}
}