简体   繁体   English

ServiceStack Runner可以获取请求正文吗?

[英]Can ServiceStack Runner Get Request Body?

Is it possible, without major hackery, to get the raw request body of a ServiceStack request within the Runner? 是否有可能在没有重大hackery的情况下在Runner中获取ServiceStack请求的原始请求主体?

I am writing an oauth service provider to run on top of ServiceStack using the new API (Service and Runner). 我正在编写一个oauth服务提供程序,使用新的API(Service and Runner)在ServiceStack上运行。 Due to how OAuth signing works I need to get the raw request body for each request. 由于OAuth签名的工作方式,我需要为每个请求获取原始请求正文。 The OAuth protection layer is added to the Runner so that an invalid OAuth request can easily return an empty/error response without any boilerplate in the Service class or subclassing a special "OAuthService" class. OAuth保护层已添加到Runner中,因此无效的OAuth请求可以轻松返回空/错误响应,而无需Service类中的任何样板或子类化特殊的“OAuthService”类。

The way to access the Raw Request Body is to use IHttpRequest.GetRawBody() or read from IHttpRequest.InputStream . 访问Raw Request Body的方法是使用IHttpRequest.GetRawBody()或从IHttpRequest.InputStream读取。

But as the HTTP Request body is a forward only stream, by default it can only be called once which is usually called by ServiceStack to deserialize the Request DTO. 但由于HTTP请求主体是仅前向流,因此默认情况下它只能被调用一次,这通常由ServiceStack调用以反序列化请求DTO。 The Serialization and Deserialization docs show how to tell ServiceStack to skip deserializing the Request and inject the unread Request Stream into the Request DTO with: 序列化和反序列化文档显示如何告诉ServiceStack跳过反序列化请求并将未读请求流注入请求DTO:

public class Hello : IRequiresRequestStream
{
    /// <summary>
    /// The raw Http Request Input Stream
    /// </summary>
    Stream RequestStream { get; set; }
}

If you still want ServiceStack to deserialize the Request DTO but also access the raw request body you need to tell ServiceStack to buffer the request before its read, which you can do by adding the PreRequestFilter: 如果您仍希望ServiceStack反序列化Request DTO,但也访问原始请求体,则需要告诉ServiceStack在读取之前缓冲请求,您可以通过添加PreRequestFilter来执行此操作:

appHost.PreRequestFilters.Insert(0, (httpReq, httpRes) => {
    httpReq.UseBufferedStream = true;
});

Which now lets you call httpReq.GetRawBody() multiple times or read directly from the IHttpRequest.InputStream since it's now buffered. 现在允许您多次调用httpReq.GetRawBody()或直接从IHttpRequest.InputStream读取,因为它现在已缓冲。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM