Project

General

Profile

updating custom fields through API using curl commands

Added by James R almost 11 years ago

Hello,

I want to be able to update custom fields within a project through the API using curl. Currently I can do this no problem when calling an external xml file:

curl -v -H "Content-Type: application/xml" -X PUT --data "@source.xml" -u user:password localhost/redmine/projects/9.xml

where source.xml =

<?xml version="1.0"?>
<project><id>9</id>
<name>test1</name>
<identifier>test1</identifier>
<description>none</description>
<custom_fields type="array">
<custom_field id="1" name="Donor">
<value>foobar</value>
</custom_field>
</custom_fields>
</project>

I would like to make the same call directly from the command line without calling an external file. I have tried the following:

curl -v -H PUT "Content-Type: application/xml" -X  -d "<project><id>9</id><name>test1</name><identifier>test1</identifier><description>none</description><custom_fields type="array"><custom_field id="1" name="Donor"><value>barfoo</value></custom_field></custom_fields></project>" -u user:password localhost/redmine/projects/9.xml

Unfortunately it fails to work and I get a 502 Proxy error. I think this is something to do with the custom field because if I remove it from the PUT request the other fields update without problem.

Can anybody point out what I am doing wrong? I am really blocked on this one.

Many thanks

Environment:
  Redmine version                          2.3.0.stable
  Ruby version                             1.9.3 (i386-mingw32)
  Rails version                            3.2.13
  Environment                              production
  Database adapter                         Mysql2
Redmine plugins:
  AgileDwarf                               0.0.3
  redmine_importer                         1.0

Replies (1)

RE: updating custom fields through API using curl commands - Added by Jean-Baptiste Barth almost 11 years ago

Basically the problem is with your command I think: as you have interlaced quotes, the shell doesn't know how to parse it and sends unfinished XML to the server, resulting in a 5xx error, because the XML you send is not valid. The other problem is "PUT" should be put behind "-X" like this.

Try using simple quotes around the XML:

curl -v -H "Content-Type: application/xml" -X PUT -d '<project><id>9</id><name>test1</name><identifier>test1</identifier><description>none</description><custom_fields type="array"><custom_field id="1" name="Donor"><value>barfoo</value></custom_field></custom_fields></project>' -u user:password localhost/redmine/projects/9.xml

Second thing is you should use an API token with the command line, not a "user:password" couple. You can generate / retrieve your authentication token in "My account", in the right sidebar. Then you can use it like this:

curl -H "X-Redmine-Api-Key: 1234543655YT45Y54Y54Y5Y55b" -X PUT -d '<project><id>9</id><name>test1</name><identifier>test1</identifier><description>none</description><custom_fields type="array"><custom_field id="1" name="Donor"><value>barfoo</value></custom_field></custom_fields></project>' localhost/redmine/projects/9.xml

    (1-1/1)