public override SystemTask.Task <InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context, Encoding encoding) { var request = context.HttpContext.Request; using (var streamReader = context.ReaderFactory(request.Body, encoding)) using (JsonTextReader jsonReader = new JsonTextReader(streamReader)) { var type = context.ModelType; try { //TODO: parse json //TODO: create a simple model to allow passthrough to validation because a missing element will throw wrong error here var resource = new FhirJsonParser().Parse(jsonReader, type); return(InputFormatterResult.SuccessAsync(resource)); } catch (Exception ex) { //TODO: Remove Fhir Hack if (ex != null) { //Assuming invalid json here, see above return(InputFormatterResult.SuccessAsync(OperationOutcomeFactory.CreateInvalidRequest())); } return(InputFormatterResult.FailureAsync()); } } }
private void CheckRequestRequirements(HttpContext context) { var contentLength = context?.Request?.ContentLength; var type = context?.Request?.Method; if (new string[] { HttpMethods.Post, HttpMethods.Put }.Contains(type) && (!contentLength.HasValue || contentLength.Value == 0)) { throw new HttpFhirException("Invalid Request", OperationOutcomeFactory.CreateInvalidRequest(), HttpStatusCode.BadRequest); } }
public override void OnActionExecuting(ActionExecutingContext context) { if (!context.ModelState.IsValid) { var error = context.ModelState.First(); var message = error.Value.Errors.FirstOrDefault()?.ErrorMessage; OperationOutcome outcome = null; if (error.Key == "InputFormatter") { outcome = OperationOutcomeFactory.CreateInvalidRequest(); } else { outcome = OperationOutcomeFactory.CreateInvalidResource(error.Key, message); } throw new HttpFhirException(message, outcome, HttpStatusCode.BadRequest); } }