Home > Back-end >  Adding Bodyparameter to POST request (C# and RestSharp)
Adding Bodyparameter to POST request (C# and RestSharp)

Time:12-07

I am quite new to programming and I am meant to make a post request to Taxdoo APi. I am using the latest version of RestSharp. I tried reading their documentation but the lack of example code has me, as a beginner, crying in a corner. Here's the problem:

The API expects an array of products with the following payload format:

{
  "products": [
    {
      "description": "Couch Supreme red",
      "productIdentifier": "redcouch123",
      "commodityCode": "9394240000",
      "purchasePrice": 3200,
      "currency": "EUR"
    },
    {
      "description": "Wood table",
      "productIdentifier": "wt234bas",
      "commodityCode": "9394220000",
      "purchasePrice": 300,
      "currency": "EUR"
    }
  ]
}

How can I get the products : into the request? I tried the request.AddParameter, the request.AddOrUpdateParameter, I tried letting RestSharp do the parsing with request.AddJsonBody but I am so lost. I get BadRequest Error every time.

My code so far:

namespace ConnectToApi
{
    internal class Program
    {
        static void Main(string[] args)
        {
            var endpoint = new Uri("https://sandbox-api.taxdoo.com/products");
            using (RestClient? restClient = new(endpoint))
            {
                var request = new RestRequest();
                var authToken = ConfigurationManager.AppSettings["authToken"];
                request.AddHeader("AuthToken", authToken);
                      
                MyProduct productOne = new MyProduct
                {
                    Description = "Product 1",
                    ProductIdentifier = "GBP-120",
                    CommodityCode = "123456",
                    PurchasePrice = 40,
                    Currency = "USD"
                };
                MyProduct productTwo = new MyProduct
                {
                    Description = "Product 2",
                    ProductIdentifier = "GBP-125",
                    CommodityCode = "789101112",
                    PurchasePrice = 20,
                    Currency = "USD"
                };
                
                List<MyProduct> productList = new();
                productList.Add(productOne);
                productList.Add(productTwo);
                
                string json = JsonConvert.SerializeObject(productList,Formatting.Indented);
                request.AddStringBody(json, DataFormat.Json);
                var response = restClient.Execute(request);
            }
        }
    }

The JSONString looks like so:

[
  {
    "description": "Product 1",
    "productIdentifier": "GBP-120",
    "commodityCode": "123456",
    "purchasePrice": 40,
    "currency": "USD"
  },
  {
    "description": "Product 2",
    "productIdentifier": "GBP-125",
    "commodityCode": "789101112",
    "purchasePrice": 20,
    "currency": "USD"
  }
]

If anyone could help or send me a ressource where to look that is beginner friendly, I would be really glad. Cheers guys.

CodePudding user response:

You don't need serialization, just a wrapping class:

public class Body
{
    public List<MyProduct> Products { get; set; }
}
...
var body = new Body();
body.Products = productList;
request.AddStringBody(body, DataFormat.Json);

CodePudding user response:

you need to add a product wraping around your list before serialization. You can use an anonymous class for example

var products = new { Products = productList};
 string json = JsonConvert.SerializeObject(products);
 request.AddStringBody(json, DataFormat.Json);
 var response = restClient.Execute(request);
  • Related