【问题标题】:How to update a field with a new value using the REST API?如何使用 REST API 使用新值更新字段?
【发布时间】:2017-03-14 09:58:10
【问题描述】:

我正在使用 Podio,我希望能够使用 PODIO RESTful API 更新项目。

我更具体地说想将PUT 纯文本放入项目字段中。我已根据instructions 成功更新了相关字段。但是,更新会导致用“无值”更新字段。我不知道如何让 API 用我提供的值更新字段。这是我的代码:

var path = '/item/';
var item_id = 346397274;
var value = 'value';
var field_id = 108976076;
var update = "foo"
var update_length = update.length;

var options = {
    'host': 'api.podio.com',
    'port': 443,
    'path': path + item_id + '/value/' + field_id,
    'accept': 'application/json',
    'method': 'PUT',
    'headers': {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Content-Length': update_length,
        'Authorization': 'Bearer ' + access_token
    }
}

var req = https.request(options, function(res) {
})

req.write(update);
req.end();

我想我的请求正文必须有所不同。但是我尝试了各种组合,但我无法让它发挥作用。当我运行GET 请求时,相关字段的返回数据如下:

{ status: 'active',
  type: 'text',
  field_id: 108976076,
  label: 'Titel',
  values: [ { value: 'Old Value' } ],
  config: 
   { default_value: null,
     description: null,
     settings: { format: 'plain', size: 'small' },
     required: false,
     mapping: null,
     label: 'Titel',
     visible: true,
     delta: 0,
     hidden: false,
     unique: false },
  external_id: 'titel' }

我还尝试在请求正文中添加以下内容:var update = querystring.stringify({"value": "foo"}); 和:

var update = {"values": [querystring.stringify({
    "value": "foo"
    })]
};

var update = querystring.stringify(update);

我做错了什么?

【问题讨论】:

    标签: javascript rest https podio


    【解决方案1】:

    请尝试以下操作:

    请求正文:

    {"titel": "My test value"}
    

    请求地址:

    https://api.podio.com/item/<item_id>/value/
    

    全部作为 cURL:

    curl 
        -H "Content-Type: application/json" 
        -H "Authorization: OAuth2 <access_token>" 
        -X PUT   
        -d '{"titel": "Some updates"}'
        https://api.podio.com/item/<item_id>/value/
    

    【讨论】:

    • 谢谢。问题似乎是我使用了"Content-Type: 'application/x-www-form-urlencoded'。这适用于身份验证目的,但显然不适用于请求目的。任何想法为什么?
    • 可能是因为这个请求正文是json 而不是x-www-form
    猜你喜欢
    • 2020-03-23
    • 1970-01-01
    • 2015-04-11
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 2020-11-26
    相关资源
    最近更新 更多