Unable to send data to unfuddle API

时间:2015-07-31 20:22:23

标签: node.js api request unfuddle

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>

1 个答案:

答案 0 :(得分:1)

代码摘录的第二部分:

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);
    }
);

注意“form”键如何在第一个选项对象中(请求只接受两个参数:选项和回调:https://www.npmjs.com/package/request)。

另外,请看这里(https://www.npmjs.com/package/request#request-options-callback)如何

  • “表单”键自动将“Content-Type”标题设置为“application / x-www-form-urlencoded”,
  • 并且应将其格式化为对象(form : {"ticket": {"status": "accepted"}})而不是您尝试的“字符串”(或作为查询字符串... form : "ticket[status]=accepted")。