繁体   English   中英

RestSharp-.AddParameter是否添加到POST请求的标头或正文中?

[英]RestSharp - Does .AddParameter add to the Header or the Body for a POST request?

尝试确定RestSharp的AddParameter方法是否将参数添加到方法POST的正文或请求的标头中。

            var request = new RestRequest("/token", Method.POST);

            request.RequestFormat = DataFormat.Json;
            request.AddParameter("grant_type", "password");
            request.AddParameter("client_id", client_id);
            request.AddParameter("client_secret", client_secret);
            request.AddParameter("username", username);
            request.AddParameter("password", password);

如果发送标头,则由于API提供商即将发生更改,我们的API调用将开始失败。

我该如何确定?

自述文件中的示例所示,RestSharp提供了一个单独的AddHeader方法,用于将数据添加到请求标头。

根据此答案, AddParameter将添加具有GetOrPost类型的参数,如果是GET请求,它将添加查询参数,如果是POST请求,则将添加项目到请求正文中。

您可以在其中提供参数名称和参数值的AddParameter方法的来源, 这里您可以看到传入的类型为ParameterType.GetOrPost

对于后代,目前的来源是:

/// <summary>
/// Add the parameter to the request
/// </summary>
/// <param name="p">Parameter to add</param>
/// <returns></returns>
public IRestRequest AddParameter(Parameter p) => this.With(x => x.Parameters.Add(p));

/// <summary>
/// Adds a HTTP parameter to the request (QueryString for GET, DELETE, OPTIONS and HEAD; Encoded form for POST and PUT)
/// </summary>
/// <param name="name">Name of the parameter</param>
/// <param name="value">Value of the parameter</param>
/// <returns>This request</returns>
public IRestRequest AddParameter(string name, object value)
    => AddParameter(new Parameter(name, value, ParameterType.GetOrPost));

作为额外的信息,可在此处获得AddHeader方法的源以及有关如何处理这些HttpHeader参数类型的文档

暂无
暂无

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

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