Project

General

Profile

Can't add project via REST

Added by Matt Matt over 10 years ago

Hello,

I've been struggling with adding a project via REST. I can get the project lists without trouble, but no matter what I send in the POST request I get a 422 error and the message that all the parameters were blank. Here is my code using RestSharp

public void AddProject(string projectName)
        {
            _error = "";
            _client.Authenticator = new HttpBasicAuthenticator("user", "password");
            var request = new RestRequest("projects.xml", Method.POST);
            request.RequestFormat = DataFormat.Xml;
            request.AddHeader("Content-type", "application/xml");
            request.AddHeader("Accept", "application/xml");

            var myObject = new
            {
                name = projectName,
                identifier = projectName,
                description = projectName
            };

            request.AddParameter("project", myObject);

            _client.ExecuteAsync(request, response =>
            {
                AddProjectCallback(response);
            });

        }

In the RedMine log I see that it got the request and found some parameters but for some reason it does not use them, so I'm thinking the format is wrong.

Started POST "/redmine/projects.xml" for 127.0.0.1 at 2013-09-24 11:45:05 -0400
Processing by ProjectsController#create as XML
  Parameters: {"project"=>"{ name = matt, identifier = matt, description = matt }"}
WARNING: Can't verify CSRF token authenticity
  Current user: cwoody (id=1)
  Rendered common/error_messages.api.rsb (0.0ms)
Expire fragment views/localhost:8080/redmine/robots.txt (0.0ms)
Completed 422 Unprocessable Entity in 1400ms (Views: 1.0ms | ActiveRecord: 139.0ms)

And the response is an array of errors.

<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<errors type=\"array\">
<error>Name can't be blank</error>
<error>Identifier can't be blank</error>
<error>Identifier is too short (minimum is 1 characters)</error>
</errors>

The API doc is a little vague but I'm assuming an xml document should work.

project (required): a hash of the project attributes, including:
name (required): the project name
identifier (required): the project identifier
description

Is there anything obviously wrong with my code? Possibly there could be an issue with the outbound xml doc but I haven't found a way to view it yet. I tried replacing "AddParameter" with "AddBody" and would just get an exception.

System.Xml.XmlException was unhandled by user code 
Message=Name cannot begin with the '<' character, hexadecimal value 0x3C.

Thanks for your help

Matt


Replies (1)

RE: Can't add project via REST - Added by Eric Sabelhaus over 10 years ago

Are you passing the information as JSON or XML?

JSON: {
"project": {
"name": "Testing REST",
"identifier": "testrest",
"description": "Testing 123"
}
}

XML:
<project>
<name>Testing REST</name>
<identifier>testrest</identifier>
<description>Testing 123</description>
</project>

    (1-1/1)