ServiceStack Runner 可以获取请求正文吗? [英] Can ServiceStack Runner Get Request Body?

查看:26
本文介绍了ServiceStack Runner 可以获取请求正文吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能在没有主要黑客的情况下在 Runner 中获取 ServiceStack 请求的原始请求正文?

Is it possible, without major hackery, to get the raw request body of a ServiceStack request within the Runner?

我正在编写一个 oauth 服务提供者,以使用新的 API(服务和运行器)在 ServiceStack 之上运行.由于 OAuth 签名的工作方式,我需要获取每个请求的原始请求正文.Runner 中添加了 OAuth 保护层,因此无效的 OAuth 请求可以轻松返回空/错误响应,而无需在 Service 类中使用任何样板或子类化特殊的OAuthService"类.

I am writing an oauth service provider to run on top of ServiceStack using the new API (Service and Runner). Due to how OAuth signing works I need to get the raw request body for each request. 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.

推荐答案

Raw Request Body 的访问方式是使用 IHttpRequest.GetRawBody() 或者从 IHttpRequest.InputStream<中读取/代码>.

The way to access the Raw Request Body is to use IHttpRequest.GetRawBody() or read from IHttpRequest.InputStream.

但由于 HTTP 请求主体是仅转发流,默认情况下只能调用一次,通常由 ServiceStack 调用以反序列化请求 DTO.序列化和反序列化文档展示了如何告诉 ServiceStack 跳过反序列化请求并注入未读的请求流到请求 DTO 中:

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. 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:

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

如果您仍然希望 ServiceStack 反序列化请求 DTO 并访问原始请求正文,您需要告诉 ServiceStack 在读取之前缓冲请求,您可以通过添加 PreRequestFilter 来实现:

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:

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

现在允许您多次调用 httpReq.GetRawBody() 或直接从 IHttpRequest.InputStream 读取,因为它现在已缓冲.

Which now lets you call httpReq.GetRawBody() multiple times or read directly from the IHttpRequest.InputStream since it's now buffered.

这篇关于ServiceStack Runner 可以获取请求正文吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆