简体   繁体   中英

C# RestSharp PUT request not sending

So I'm trying to send a PUT request to my API to edit one of my records in one of my tables. This woks fine with Postman and the swagger page. But somehow it doesn't send when I'm trying to do this with C#'s webclient. This is my code.

public static void putProductData(string model, Models.Product product)
    {
        //HttpClient client = new HttpClient();
        MultipartFormDataContent mfdc = new MultipartFormDataContent();

        // create the communication to the model from the API.
        string apiposturl = apiurl;
        apiposturl += model;
        apiposturl += "/bewerken";

        var client = new RestClient("http://mayapi.net");
        var request = new RestRequest("/api/products/bewerken", Method.PUT);

        // Zet de headers voor de request.
        // Dit is bij alles hetzelfde met een multipart/form-data requeset.
        request.AddHeader("postman-token", "293a9ff3-e250-e688-e20d-5d16ea635597");
        request.AddHeader("cache-control", "no-cache");
        request.AddHeader("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
        request.AddHeader("Content-Type", "multipart/form-data");
        request.AddHeader("Authorization", "token");
        request.AddHeader("Connection", "Keep-Alive");

        // Vul de parameters met de waardes van heet model.
        request.AddParameter("productNaam", product.ProductNaam);
        request.AddParameter("productPrijs", product.ProductPrijs);
        request.AddParameter("productBeschrijving", product.ProductBeschrijving);
        request.AddParameter("productType", product.ProductType);
        request.AddParameter("productKorting", product.ProductKorting);
        request.AddParameter("productVoorraad", product.ProductVoorraad);
        request.AddParameter("productDirectLeverbaar", product.ProductDirectLeverbaar);
        request.AddFile("productAfbeelding", product.ProductAfbeelding); // Voeg hier het bestandspad in.
        request.AddParameter("productWinkel", product.ProductWinkel);


        //client.Execute(request);
        // Verstuur de request.
        //IRestResponse response = client.Execute(request);

        IRestResponse response = client.Execute(request);
        var content = response.Content; ;
    }

The weird thing is that when I execute this that there's no PUT request in Fiddler, so I can't really debug it through Fiddler. This is one of the errors the response shows in the code with breakpoints:

{"The request was aborted: The request was canceled."}

I read that this could be fixed with Connection: Keep-Alive, but this sadly doesn't work for me. Can anybody please help me out?

EDIT

I thought this might be some useful information. if I send a post request with RestSharp it actually works, but the PUT request doesn't

I solved it, it had to do something with my updated models in the API. They guy working on this code forgot to update the fields to the updated model. This is the reason why it wouldn't send the data.

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