如何添加文本到RestSharp请求体文本、RestSharp

2023-09-03 08:34:44 作者:〓     少年一梦

我试图用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);

怎样给PDF文本添加边框

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);