Frage

I'm trying to implement https://developers.podio.com/doc/items/add-new-item-22362 Podio API addItem call in a nodejs module. Here is the code:

var _makeRequest = function(type, url, params, cb) {
  var headers = {};
  if(_isAuthenticated) {
    headers.Authorization = 'OAuth2 ' + _access_token ;
  }
  console.log(url,params);
  _request({method: type, url: url, json: true, form: params, headers: headers},function (error, response, body) {
    if(!error && response.statusCode == 200) {
      cb.call(this,body);
    } else {
      console.log('Error occured while launching a request to Podio: ' + error + '; body: ' + JSON.stringify (body));
    }
  });
}

exports.addItem = function(app_id, field_values, cb) {
  _makeRequest('POST', _baseUrl + "/item/app/" + app_id + '/',{fields: {'title': 'fgdsfgdsf'}},function(response) {
    cb.call(this,response);
  });

It returns the following error:

{"error_propagate":false,"error_parameters":{},"error_detail":null,"error_description":"No matching operation could be found. No body was given.","error":"not_found"}

Only "title" attribute is required in the app - I checked that in Podio GUI. I also tried to remove trailing slash from the url where I post to, then a similar error occurs, but with the URL not found message in the error description.

I'm going to setup a proxy to catch a raw request, but maybe someone just sees the error in the code?

Any help is appreciated.

War es hilfreich?

Lösung

Nevermind on this, I found a solution. The thing is that addItem call was my first "real"-API method implementation with JSON parameters in the body. The former calls were authentication and getApp which is GET and doesn't have any parameters.

The problem is that Podio supports POST key-value pairs for authentication, but doesn't support this for all the calls, and I was trying to utilize single _makeRequest() method for all the calls, both auth and real-API ones.

Looks like I need to implement one for auth and one for all API calls.

Anyway, if someone needs a working proof of concept for addItem call on node, here it is (assuming you've got an auth token beforehand)

_request({method: 'POST', url: "https://api.podio.com/item/app/" + app_id + '/', headers: headers, body: JSON.stringify({fields: {'title': 'gdfgdsfgds'}})},function(error, response, body) {
  console.log(body);
});

Andere Tipps

  • You should set content-type to application/json
  • send the body as stringfied json.

    const getHeaders = async () => {
      const headers = {
        Accept: 'application/json',
        'Content-Type': 'application/json; charset=utf-8',
      };
    
      const token = "YOUR APP TOKEN HERE";
      headers.Authorization = `Bearer ${token}`;
    
      return headers;
    }
    
    
    const createItem = async (data) => {
        const uri = `https://api.podio.com/item/app/${APP_ID}/`;
        const payload = {
            fields: {
              [data.FIELD_ID]: [data.FIELD_VALUE],
            },
        };
        const response = await fetch(uri, {
            method: 'POST',
            headers: await getHeaders(),
            body: JSON.stringify(payload),
        });
    
        const newItem = await response.json(); 
        return newItem;
    }
    
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top