简体   繁体   中英

Is it possible to remove quotations from AddParameter method RestSharp?

I've ran into a problem with RestSharp. I need to make a POST call but with the quotation marks it doesn't work.

The JSON needs to look like this:

{
  "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}]
}

But when I send my request it is received as this: causing it to fail.

{
   "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}]"
}

My code for this call is the following:

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

Now i've seen a number of posts all over the internet saying you need to use AddBody or AddJsonBody instead of the AddParameter, but the receiver of my POST call doesn't accept "application/json" as a name, so that isn't working.

Is it possible with RestSharp to achieve what I want or do I have to find an alternative? If so, can you point me in the right direction?

Cheers and thanks in advance!

I've figured it out. Here's the answer if anyone ever stumbles acros the same issue:

I stopped trying to fix it with RestSharp. Instead I used HttpClient(). I added the api_key, controller and action to the InvoiceToSend model;

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

Then filled it with the data i needed;

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

Then made the POST call using HttpClient():

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

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

Hope I can help someone with this in the future ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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