public void GetFoo(Context context) { Test test = new Test() { Status = System.Net.HttpStatusCode.OK, Username = "******", Nickname = "AmazingTurtle", Email = "*****@*****.**", Child = new Test() { Status = System.Net.HttpStatusCode.OK, Username = "******", Nickname = "*****@*****.**", Email = "*****@*****.**" } }; context.Response = Response.Json(test); }
public void PostSomething(Context context) { GlobalConfiguration.GetInstance().Logger.InfoFormat("username: {0}, password: {1}", context.Request.Post("username"), context.Request.Post("password")); context.Response = Response.Error(System.Net.HttpStatusCode.OK); // we're fine }
public void GetSomething(Context context, int testParam1, string testParam2) { string getParam = context.Request.Get("test") ?? "default"; context.Response = Response.Text("Hello, " + getParam + ". testParam1: " + testParam1.ToString() + ", testParam2: " + testParam2); }
/// <summary> /// Give response if methodInvocationDelegate set one; if not, give an error response /// </summary> private void giveResponse(ResourceMethod resourceMethod, Context invocationContext, IHttpResponseDelegate response, object[] parameters) { object[] completeParameters = new Object[] { invocationContext }.Concat(parameters).ToArray(); resourceMethod.Method.DynamicInvoke(completeParameters); if (invocationContext.Response != null) response.OnResponse(invocationContext.Response.Head, invocationContext.Response.Body); else { Response conflictResponse = Response.Error(HttpStatusCode.Conflict); response.OnResponse(conflictResponse.Head, conflictResponse.Body); } }
/// <summary> /// IHttpRequestHandler.OnRequest internal handler /// </summary> internal void onRequest(HttpRequestHead head, IDataProducer body, IHttpResponseDelegate response) { RESTMethod method; if (Enum.TryParse<RESTMethod>(head.Method, out method)) { Pair<ResourceInstance, object[]> resourceParameterPair = retrieveResource(head.Path, method); ResourceInstance resource = resourceParameterPair.V1; object[] resourceParameters = resourceParameterPair.V2; if (resource != null) { ResourceMethod resourceMethod = resource.GetResourceMethod(method); if (resourceMethod != null) { if (resourceMethod.Parameters.Length == resourceParameters.Length + 1) { NameValueCollection getQuery = HttpUtility.ParseQueryString(head.QueryString ?? ""); // now, if it's a POST, read POST data then if (method == RESTMethod.POST) { var consumer = new BufferedConsumer(bufferedBody => { HttpMultipartParser.MultipartFormDataParser parser = new HttpMultipartParser.MultipartFormDataParser(new MemoryStream(Encoding.Default.GetBytes(bufferedBody))); NameValueCollection postQuery = new NameValueCollection(); foreach (var param in parser.Parameters) postQuery.Add(param.Key, param.Value.Data); Context invocationContext = new Context(new Request(head, postQuery, getQuery)); giveResponse(resourceMethod, invocationContext, response, resourceParameters); }, error => { _logger.Error("IDataProducer.Connect failed", error); }); body.Connect(consumer); } else { Context invocationContext = new Context(new Request(head, getParameters: getQuery)); giveResponse(resourceMethod, invocationContext, response, resourceParameters); } } else { Response notFoundResponse = Response.Error(HttpStatusCode.NotFound); response.OnResponse(notFoundResponse.Head, notFoundResponse.Body); } } else { Response notImplementedResponse = Response.Error(HttpStatusCode.NotImplemented); response.OnResponse(notImplementedResponse.Head, notImplementedResponse.Body); } } else { Response notFoundResponse = Response.Error(HttpStatusCode.NotFound); response.OnResponse(notFoundResponse.Head, notFoundResponse.Body); } } else { Response methodNotAllowedResponse = Response.Error(HttpStatusCode.MethodNotAllowed); response.OnResponse(methodNotAllowedResponse.Head, methodNotAllowedResponse.Body); } }
public void getSomething(Context context) { context.Response = Response.Text("Foo resource!"); }