FubarDevelopment / restsharp.portable

Some kind of a RestSharp port to PCL
BSD 2-Clause "Simplified" License
96 stars 33 forks source link

Object reference not set to an instance of an object when trying to do a PUT request #58

Closed Alexisvt closed 8 years ago

Alexisvt commented 8 years ago

Hi! First thanks for the project! I think is great!! I'm having a problem when I try to do a PUT request to a Rest Service that have OAuth 1 authentication. This is the example code:

using (var client = new RestClient("http://localhost:8080"))
            {
                client.Authenticator = OAuth1Authenticator
                    .ForAccessToken(_consumerKey, _consumerSecret, _accesToken, _accesTokenSecret);

                var testObj = new
                {
                    product = new { id = 1, sku = "24-MB01", name = "David" },
                    saveOptions = true
                };

                var request = new RestRequest("magento/rest/V1/products/24-MB01", Method.PUT);
                request.AddHeader("content-type", "application/json");
                request.AddJsonBody(testObj);

                //here is where I have the object reference problem
                var result = await client.Execute<object>(request);
            }

I already did a test with do a PUT request using POSTMAN 2 and it works! But when I try tod do the same thing but with RestSharp I'm getting an exception! Maybe I need to something more but currently I don't know how to do it properly because I can't find examples of this kind of operations. Any help I will be really grateful! By the way, when I do a simple GET request RestSharp works like a charm, in that operation I don't need the OAuth part.

Alexisvt commented 8 years ago

Sorry I discovered my issue, the problem was that I was using the wrong add method. In the example I used AddJsonBody and that was wrong, the correct one is AddObject.