Conrad C
Conrad C

Reputation: 746

Cannot PUT/POST using Rest Sharp using XML

I want to create/modify an issue on redmine using the PUT/POST methods of restSharp. I cannot find valuable information about xml PUT/POST using Rest sharp. I tried various methods from restsharp.org like Addbody("test", "subject"); , IRestResponse response = client.Execute(request); but there is no change in Redmine. What am I doing wrong?

POST gives a "Only get, put, and delete requests are allowed." message.

PUT gives a "Only get, post, and delete requests are allowed." message.

My Code

    RestClient client = new RestClient(_baseUrl);
    client.Authenticator = new HttpBasicAuthenticator(_user, _password);


    RestRequest request = new RestRequest("issues/{id}.xml", Method.POST);

    request.AddParameter("subject", "Testint POST");

    request.AddUrlSegment("id", "5");


    var response = client.Execute(request);

Upvotes: 1

Views: 3624

Answers (2)

Conrad C
Conrad C

Reputation: 746

The problem was in the serialization. My Issue class contains object of various other classes which was causing a problem in the serialization. This is how we did it:

    RestRequest request = new RestRequest("issues/{id}.xml", Method.PUT);
    request.AddParameter("id", ticket.id, ParameterType.UrlSegment);
    request.XmlSerializer = new RedmineXmlSerializer();
    request.AddBody(ticket);

    RestClient client = new RestClient(_baseUrl);
    client.Authenticator = new HttpBasicAuthenticator(_user, _password);
    IRestResponse response = client.Execute(request);

Upvotes: 1

C.M.
C.M.

Reputation: 1561

Your code looks ok to me, I'm unsure if you need this but we added this header when using RestSharp for json against a WebAPI host:

        request.AddHeader("Accept", "application/xml");

Upvotes: 0

Related Questions