簡體   English   中英

是否可以從 AddParameter 方法 RestSharp 中刪除引號?

[英]Is it possible to remove quotations from AddParameter method RestSharp?

我遇到了 RestSharp 的問題。 我需要進行 POST 調用,但使用引號不起作用。

JSON 需要如下所示:

{
  "api_key": "<api key>",
  "controller": "invoice",
  "action": "add",
    "DebtorCode": "DB10000",
    "Term": 14,
    "InvoiceLines": [{"ProductCode":"P0001","Number":15},{"ProductCode":"P0002","Number":15},{"ProductCode":"P0003","Number":15}]
}

但是當我發送我的請求時,它被接收為:導致它失敗。

{
   "api_key": "<api key>",
   "controller": "invoice",
   "action": "add",
   "DebtorCode": "DB10000",
   "Term": "14",
   "InvoiceLines": "[{\"ProductCode\":\"P0001\",\"Number\":15},{\"ProductCode\":\"P0002\",\"Number\":15},{\"ProductCode\":\"P0003\",\"Number\":15}]"
}

我這次調用的代碼如下:

            var newInvoice = new InvoiceToSend();

            newInvoice.DebtorCode = invoiceItem.DebtorCode;
            newInvoice.Term = Convert.ToInt32(invoiceItem.Term);
            newInvoice.InvoiceLines = new List<InvoiceLines>();

            for (int i = 0; i < invoiceItem.InvoiceLines.Count; i++)
            {
                var newLine = new InvoiceLines();

                newLine.Number = Convert.ToInt32(invoiceItem.InvoiceLines[i].Number);
                newLine.ProductCode = invoiceItem.InvoiceLines[i].ProductCode;
                
                newInvoice.InvoiceLines.Add(newLine);
            }

            object invoiceLines = JsonConvert.SerializeObject(newInvoice.InvoiceLines);

            var request = new RestRequest(Method.POST)
            {
                AlwaysMultipartFormData = true
            };
            request.AddParameter("api_key", _options.ApiKey, ParameterType.GetOrPost);
            request.AddParameter("controller", "invoice", ParameterType.GetOrPost);
            request.AddParameter("action", "add", ParameterType.GetOrPost);
            request.AddParameter("DebtorCode", newInvoice.DebtorCode);
            request.AddParameter("Term", newInvoice.Term);
            request.AddParameter("InvoiceLines", invoiceLines);

            var response = Client.Execute(request);

現在我在互聯網上看到很多帖子說你需要使用 AddBody 或 AddJsonBody 而不是 AddParameter,但是我的 POST 調用的接收者不接受“application/json”作為名稱,所以這不是工作。

RestSharp 是否有可能實現我想要的目標,或者我是否必須找到替代方案? 如果是這樣,你能指出我正確的方向嗎?

提前干杯和感謝!

我已經想通了。 如果有人遇到同樣的問題,這里是答案:

我停止嘗試用 RestSharp 修復它。 相反,我使用了 HttpClient()。 我在 InvoiceToSend 模型中添加了 api_key、控制器和動作;

public class InvoiceToSend
{
    public string api_key { get; set; }
    public string controller { get; set; }
    public string action { get; set; }
    public string DebtorCode { get; set; }
    public int Term { get; set; }
    public List<InvoiceLines> InvoiceLines { get; set; }
}

public class InvoiceLines
{
    public string ProductCode { get; set; }
    public int Number { get; set; }
}

然后用我需要的數據填充它;

            var newInvoice = new InvoiceToSend();

            newInvoice.api_key = _options.ApiKey;
            newInvoice.controller = "invoice";
            newInvoice.action = "add";
            newInvoice.DebtorCode = invoiceItem.DebtorCode;
            newInvoice.Term = Convert.ToInt32(invoiceItem.Term);
            newInvoice.InvoiceLines = new List<InvoiceLines>();

            for (int i = 0; i < invoiceItem.InvoiceLines.Count; i++)
            {
                var newLine = new InvoiceLines();

                newLine.Number = Convert.ToInt32(invoiceItem.InvoiceLines[i].Number);
                newLine.ProductCode = invoiceItem.InvoiceLines[i].ProductCode;
                
                newInvoice.InvoiceLines.Add(newLine);
            }

然后使用 HttpClient() 進行 POST 調用:

            var client = new HttpClient();
            var json = JsonConvert.SerializeObject(newInvoice);
            var data = new StringContent(json);

            var response = await client.PostAsync(_options.BaseUrl, data);

希望我將來能幫助別人解決這個問題;)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM