ServiceStack亚军可以得到请求体? [英] Can ServiceStack Runner Get Request Body?

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

问题描述

是否有可能,没有重大两轮牛车,拿到亚军?

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

我写一个OAuth服务提供商在使用新的API(服务和亚军)ServiceStack上运行。由于OAuth的是如何工作的签约,我需要得到的原始请求主体为每个请求。该OAuth的保护层被添加到亚军,这样一个无效的OAuth请求可以很容易地返回,而不在服务类中的任何样板或继承特殊的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.

推荐答案

访问原始请求体的方法是使用 IHtt prequest.GetRawBody()或者从 IHtt prequest.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;
});

现在,您可以拨打 HTT preq.GetRawBody()多次,或者直接从 IHtt prequest.InputStream阅读,因为它现在的缓冲。

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

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

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