【问题标题】:Unable to send data to unfuddle API无法将数据发送到 unfudle API
【发布时间】:2015-10-23 15:06:36
【问题描述】:
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);
    }
);

我正在尝试使用 node.js 请求更新 unfuddle 票证的状态。如何用数据格式化帖子。票的数据存储在一个 xml 文件中(在 unfuddle 服务器上)见下文。当我使用 node.js 在终端中编写脚本时,它会等待其他命令,并且在 unfuddle 上没有任何更新。 任何建议将不胜感激。

<?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>

【问题讨论】:

    标签: node.js api request unfuddle


    【解决方案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) 方法

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-02
      • 2017-06-09
      • 1970-01-01
      • 2021-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多