如何添加文本到RestSharp请求体 [英] How to add text to request body in RestSharp

查看:1208
本文介绍了如何添加文本到RestSharp请求体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用RestSharp来使用Web服务。到目前为止,一切都非常顺利(欢呼约翰·希恩和所有贡献者!),但我已经遇到了障碍。说我想插入XML到我在已经连载形式RestRequest的主体(即作为字符串)。有没有一种简单的方法来做到这一点?它出现在.AddBody()函数进行序列化屁股的场景,让我的字符串正在变成。

任何帮助是极大AP preciated!

编辑:有人要求我目前的code的样本。见下面 -

 私人牛逼ExecuteRequest< T>(字符串资源,
                            RestSharp.Method httpMethod,
                            IEnumerable的<参数>参数= NULL,
                            绳体= NULL)其中T:新的()
{
    RestClient客户端=新RestClient(this.BaseURL);
    RestRequest REQ =新RestRequest(资源,httpMethod);

    //所有参数(和身体,如果适用的话)添加到该请求
    req.AddParameter(API_KEY,this.APIKey);
    如果(参数!= NULL)
    {
        的foreach(在参数参数p)req.AddParameter(P);
    }

    (!string.IsNullOrEmpty(体))如果req.AddBody(体); //<  - 问题在这里

    RestResponse< T> RESP = client.Execute< T>(REQ);
    返回resp.Data;
}
 

解决方案

下面是如何纯XML字符串添加到请求体:

req.AddParameter(为text / xml,身体,ParameterType.RequestBody);

I'm trying to use RestSharp to consume a web service. So far everything's gone very well (cheers to John Sheehan and all contributors!) but I've run into a snag. Say I want to insert XML into the body of my RestRequest in its already serialized form (i.e., as a string). Is there an easy way to do this? It appears the .AddBody() function conducts serialization behinds the scenes, so my string is being turned into "".

Any help is greatly appreciated!

EDIT: A sample of my current code was requested. See below --

private T ExecuteRequest<T>(string resource,
                            RestSharp.Method httpMethod,
                            IEnumerable<Parameter> parameters = null,
                            string body = null) where T : new()
{
    RestClient client = new RestClient(this.BaseURL);
    RestRequest req = new RestRequest(resource, httpMethod);

    // Add all parameters (and body, if applicable) to the request
    req.AddParameter("api_key", this.APIKey);
    if (parameters != null)
    {
        foreach (Parameter p in parameters) req.AddParameter(p);
    }

    if (!string.IsNullOrEmpty(body)) req.AddBody(body); // <-- ISSUE HERE

    RestResponse<T> resp = client.Execute<T>(req);
    return resp.Data;
}

解决方案

Here is how to add plain xml string to the request body:

req.AddParameter("text/xml", body, ParameterType.RequestBody);

这篇关于如何添加文本到RestSharp请求体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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