服务堆栈 - 尝试创建 POST 函数并读取 JSON 数据
Service Stack - Trying to create a POST function and read the JSON data
我只是想创建一个简单的 POST 函数,让我 POST 一个 JSON。我试过复制例子,但我不确定我在做什么不同。任何帮助将不胜感激,我觉得这是我所缺少的简单东西。
我正在尝试post:
POST 地址:http://localhost:49653/save/file
Headers:
Content-Type: application/json
原始Body:
{
uuid: "someUuid",
文件名:"test",
日期时间:"dateee",
json: "some json"
}
namespace SomeNamespace.Model
{
[Route("/save/file", "POST")]
public class SaveFileRequest
{
public Stream RequestStream { get; set; }
}
public class SaveFileResponse
{
public bool Success { get; set; }
}
}
namespace SomeNamespace.ServiceInterface
{
[EnableCors(allowedMethods:"POST")]
public class SaveFileService : Service
{
public object Any(SaveFileRequest request)
{
var response = new SaveFileResponse { Success = false };
string savedataJson;
using (var reader = new StreamReader(Request.InputStream))
{
savedataJson = reader.ReadToEnd();
}
try
{
Console.WriteLine(savedataJson); // When I debug, the contents are ""
}
catch(Exception ex) {...}
}
}
}
}
你的SaveFileRequest
请求DTO需要实现IRequiresRequestStream
.
这是 reading directly from the request stream 的文档:
直接从请求流中读取
您可以跳过请求 DTO 的序列化而不是注册自定义绑定器,您可以添加 IRequiresRequestStream 接口直接检索流而不填充请求 DTO。
//Request DTO
public class RawBytes : IRequiresRequestStream
{
/// <summary>
/// The raw Http Request Input Stream
/// </summary>
Stream RequestStream { get; set; }
}
这告诉 ServiceStack 跳过尝试反序列化请求,这样您就可以自己读取原始 HTTP 请求正文,例如:
public object Post(RawBytes request)
{
byte[] bytes = request.RequestStream.ReadFully();
string text = bytes.FromUtf8Bytes(); //if text was sent
}
我只是想创建一个简单的 POST 函数,让我 POST 一个 JSON。我试过复制例子,但我不确定我在做什么不同。任何帮助将不胜感激,我觉得这是我所缺少的简单东西。
我正在尝试post:
POST 地址:http://localhost:49653/save/file
Headers: Content-Type: application/json
原始Body: { uuid: "someUuid", 文件名:"test", 日期时间:"dateee", json: "some json" }
namespace SomeNamespace.Model
{
[Route("/save/file", "POST")]
public class SaveFileRequest
{
public Stream RequestStream { get; set; }
}
public class SaveFileResponse
{
public bool Success { get; set; }
}
}
namespace SomeNamespace.ServiceInterface
{
[EnableCors(allowedMethods:"POST")]
public class SaveFileService : Service
{
public object Any(SaveFileRequest request)
{
var response = new SaveFileResponse { Success = false };
string savedataJson;
using (var reader = new StreamReader(Request.InputStream))
{
savedataJson = reader.ReadToEnd();
}
try
{
Console.WriteLine(savedataJson); // When I debug, the contents are ""
}
catch(Exception ex) {...}
}
}
}
}
你的SaveFileRequest
请求DTO需要实现IRequiresRequestStream
.
这是 reading directly from the request stream 的文档:
直接从请求流中读取
您可以跳过请求 DTO 的序列化而不是注册自定义绑定器,您可以添加 IRequiresRequestStream 接口直接检索流而不填充请求 DTO。
//Request DTO
public class RawBytes : IRequiresRequestStream
{
/// <summary>
/// The raw Http Request Input Stream
/// </summary>
Stream RequestStream { get; set; }
}
这告诉 ServiceStack 跳过尝试反序列化请求,这样您就可以自己读取原始 HTTP 请求正文,例如:
public object Post(RawBytes request)
{
byte[] bytes = request.RequestStream.ReadFully();
string text = bytes.FromUtf8Bytes(); //if text was sent
}