简体   繁体   中英

Unable to send data to unfuddle API

var request = require('request'),
    username = "someUSerName",
    password = "somePassWord",
    url = 'https://evergladesolutions.unfuddle.com/api/v1/projects/37236/tickets/by_number/673.xml',
    auth = "Basic " + new Buffer(username + ":" + password).toString("base64");

request.put(
    {
        url : url,
        headers : {
            "Authorization" : auth,
            'Accept': 'application/xml',
            'content-type': 'application/x-www-form-urlencoded'
        }
    },form: {
           string : '<ticket><status>accepted</status></ticket>' 
            }, 
    function (error, response, body) { 
         console.log(response.statusCode);
         //var info = JSON.parse(body);
         console.log(body);
    }
);

I am trying to update the status on unfuddle ticket using node.js request. How do I format the post with data. The data for the ticket is stored in a xml file(on unfuddle servers) see below. When I the script it in the terminal with node.js, it awaits additional commands and nothing is updated on unfuddle. Any suggestions would be much appreciated.

<?xml version="1.0" encoding="UTF-8"?>
<ticket>
  <assignee-id type="integer" nil="true"></assignee-id>
  <component-id type="integer">45937</component-id>
  <field1-value-id type="integer">1961</field1-value-id>
  <field2-value-id type="integer">1959</field2-value-id>
  <field3-value-id type="integer">1958</field3-value-id>
  <id type="integer">588430</id>
  <milestone-id type="integer" nil="true"></milestone-id>
  <number type="integer">673</number>
  <priority>3</priority>
  <project-id type="integer">37236</project-id>
  <reporter-id type="integer">45511</reporter-id>
  <resolution></resolution>
  <resolution-description></resolution-description>
  <resolution-description-format>textile</resolution-description-format>
  <severity-id type="integer" nil="true"></severity-id>
  <sort-order nil="true"></sort-order>
  <status>new</status>
  <summary>Flow 7 - Budget Quotes - Fields shown in Quote CLIEF for SKU</summary>
  <version-id type="integer" nil="true"></version-id>
  <created-at>2015-04-09T06:21:47Z</created-at>
  <updated-at>2015-04-09T06:21:47Z</updated-at>
</ticket>

The second part of the code excerpt:

request.put(
    {
        url : url,
        headers : {
            "Authorization" : auth,
            'Accept': 'application/xml'
        },
        form : {"ticket": {"status": "accepted"}}
    },
    function (error, response, body) { 
         console.log(response.statusCode);
         // var info = JSON.parse(body);
         console.log("body:", body);
    }
);

Note how the "form" key needs to be inside the first options object (request only accepts two params: options and callback: https://www.npmjs.com/package/request ).

Also, see here ( https://www.npmjs.com/package/request#request-options-callback ) how

  • the "form" key automatically sets the "Content-Type" header to "application/x-www-form-urlencoded",
  • and it should be formatted as an object ( form : {"ticket": {"status": "accepted"}} ) rather than the "string" you tried (or as a querystring... form : "ticket[status]=accepted" ).

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