Fixed serializing wrong object.
Error is now serialized too.
This commit is contained in:
parent
e9c3363313
commit
fea3be69f5
1 changed files with 14 additions and 11 deletions
|
|
@ -49,23 +49,26 @@ namespace TShockAPI
|
||||||
|
|
||||||
void OnRequest(object sender, RequestEventArgs e)
|
void OnRequest(object sender, RequestEventArgs e)
|
||||||
{
|
{
|
||||||
var coms = commands.Where(r => r.Path.ToLower().Equals(e.Request.Uri.AbsolutePath.ToLower()));
|
var obj = Process(sender, e);
|
||||||
foreach (var com in coms)
|
if (obj == null)
|
||||||
{
|
throw new NullReferenceException("obj");
|
||||||
var obj = com.Callback(e.Request.Parameters, e);
|
var str = JsonConvert.SerializeObject(obj, Formatting.Indented);
|
||||||
if (obj != null)
|
|
||||||
{
|
|
||||||
var str = JsonConvert.SerializeObject(this, Formatting.Indented);
|
|
||||||
e.Response.Connection.Type = ConnectionType.Close;
|
e.Response.Connection.Type = ConnectionType.Close;
|
||||||
e.Response.Body.Write(Encoding.ASCII.GetBytes(str), 0, str.Length);
|
e.Response.Body.Write(Encoding.ASCII.GetBytes(str), 0, str.Length);
|
||||||
e.Response.Status = HttpStatusCode.OK;
|
e.Response.Status = HttpStatusCode.OK;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected virtual object Process(object sender, RequestEventArgs e)
|
||||||
|
{
|
||||||
|
var coms = commands.Where(r => r.Path.ToLower().Equals(e.Request.Uri.AbsolutePath.ToLower()));
|
||||||
|
foreach (var com in coms)
|
||||||
|
{
|
||||||
|
var obj = com.Callback(e.Request.Parameters, e);
|
||||||
|
if (obj != null)
|
||||||
|
return obj;
|
||||||
}
|
}
|
||||||
string error = "Error: Invalid request";
|
return new Dictionary<string, string> { { "Error", "Invalid request" } };
|
||||||
e.Response.Connection.Type = ConnectionType.Close;
|
|
||||||
e.Response.Body.Write(Encoding.ASCII.GetBytes(error), 0, error.Length);
|
|
||||||
e.Response.Status = HttpStatusCode.InternalServerError;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#region Dispose
|
#region Dispose
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue