Mautic API无法创建DynamicContent

时间:2018-07-13 08:23:03

标签: node.js mautic

我正在努力在mautic REST api上成功执行create调用。在我的节点项目中,我正在使用标准的快速安装和节点api https://github.com/sambarnes90/node-mautic

的包装器

我已使用api对我的机器进行了身份验证,并且可以毫无问题地提取数据。问题是向其中发布数据。

目标发布请求的文档在这里:https://developer.mautic.org/#create-dynamic-content

这是我的路线。它来自node-mautic中的示例,该示例工作正常,但对我来说不可行:

router.get('/api', function(req, res, next) {
  //check auth and create config object
  mautic.auth.checkAuth(function(config) {
    // if all worked
    if (config.auth_object) {
      var testbody = {
        "name": "apitest2"
      }

      //create call
      mautic.dynamiccontent.createDynamicContent(config, testbody, function(data) {
        console.log(data);
        return res.send(data);
      });
      //request call for id=3
      /*mautic.dynamiccontent.getDynamicContent(config, 3, function(data) {
        console.log(data);
        return res.send(data);
        });
      });*/
    }
  });
});

我从API收到此响应:

[ { code: 400,
message: 'name: A name is required.',
details: { name: [Array] } } ]

这是为此的GitHub项目:https://github.com/raphaelurban/mautic-api-test

编辑: 我也尝试过:

var testbody = new Array();
testbody['name'] = 'apitest2';

具有相同的结果。

这是完整的POST请求:

Request {
  domain: null,
  _events:
   { error: [Function: bound ],
     complete: [Function: bound ],
     pipe: [Function] },
  _eventsCount: 3,
  _maxListeners: undefined,
  body: '{"name":"apitest2"}',
  callback: [Function],
  method: 'POST',
  readable: true,
  writable: true,
  explicitMethod: true,
  _qs:
   Querystring {
     request: [Circular],
     lib: { formats: [Object], parse: [Function], stringify: [Function] },
     useQuerystring: undefined,
     parseOptions: {},
     stringifyOptions: {} },
  _auth:
   Auth {
     request: [Circular],
     hasAuth: false,
     sentAuth: false,
     bearerToken: null,
     user: null,
     pass: null },
  _oauth: OAuth { request: [Circular], params: null },
  _multipart:
   Multipart {
     request: [Circular],
     boundary: 'afa47f83-3327-4d67-982d-4db6daab8a39',
     chunked: false,
     body: null },
  _redirect:
   Redirect {
     request: [Circular],
     followRedirect: true,
     followRedirects: true,
     followAllRedirects: false,
     followOriginalHttpMethod: false,
     allowRedirect: [Function],
     maxRedirects: 10,
     redirects: [],
     redirectsFollowed: 0,
     removeRefererHeader: false },
  _tunnel:
   Tunnel {
     request: [Circular],
     proxyHeaderWhiteList:
      [ 'accept',
        'accept-charset',
        'accept-encoding',
        'accept-language',
        'accept-ranges',
        'cache-control',
        'content-encoding',
        'content-language',
        'content-location',
        'content-md5',
        'content-range',
        'content-type',
        'connection',
        'date',
        'expect',
        'max-forwards',
        'pragma',
        'referer',
        'te',
        'user-agent',
        'via' ],
     proxyHeaderExclusiveList: [] },
  headers: { host: 'hrutest.mautic.net', 'content-length': 19 },
  setHeader: [Function],
  hasHeader: [Function],
  getHeader: [Function],
  removeHeader: [Function],
  localAddress: undefined,
  pool: {},
  dests: [],
  __isRequestRequest: true,
  _callback: [Function],
  uri:
   Url {
     protocol: 'https:',
     slashes: true,
     auth: null,
     host: 'hrutest.mautic.net',
     port: 443,
     hostname: 'hrutest.mautic.net',
     hash: null,
     search: '?access_token=ZTJlOWY3MzI0YTg4OWViNzU3YjY5YjFiZjRlOTU1OWFiYWM1N2ZmOTQ4OWI4NGI2NzZjZGUyMDg3ZWMxZmU5OA',
     query: 'access_token=ZTJlOWY3MzI0YTg4OWViNzU3YjY5YjFiZjRlOTU1OWFiYWM1N2ZmOTQ4OWI4NGI2NzZjZGUyMDg3ZWMxZmU5OA',
     pathname: '/api/dynamiccontents/new',
     path: '/api/dynamiccontents/new?access_token=ZTJlOWY3MzI0YTg4OWViNzU3YjY5YjFiZjRlOTU1OWFiYWM1N2ZmOTQ4OWI4NGI2NzZjZGUyMDg3ZWMxZmU5OA',
     href: 'https://hrutest.mautic.net/api/dynamiccontents/new?access_token=ZTJlOWY3MzI0YTg4OWViNzU3YjY5YjFiZjRlOTU1OWFiYWM1N2ZmOTQ4OWI4NGI2NzZjZGUyMDg3ZWMxZmU5OA' },
  proxy: null,
  tunnel: true,
  setHost: true,
  originalCookieHeader: undefined,
  _disableCookies: true,
  _jar: undefined,
  port: 443,
  host: 'hrutest.mautic.net',
  path: '/api/dynamiccontents/new?access_token=ZTJlOWY3MzI0YTg4OWViNzU3YjY5YjFiZjRlOTU1OWFiYWM1N2ZmOTQ4OWI4NGI2NzZjZGUyMDg3ZWMxZmU5OA',
  httpModule:
   { Server: { [Function: Server] super_: [Object] },
     createServer: [Function: createServer],
     globalAgent:
      Agent {
        domain: null,
        _events: [Object],
        _eventsCount: 1,
        _maxListeners: undefined,
        defaultPort: 443,
        protocol: 'https:',
        options: [Object],
        requests: {},
        sockets: [Object],
        freeSockets: {},
        keepAliveMsecs: 1000,
        keepAlive: false,
        maxSockets: Infinity,
        maxFreeSockets: 256,
        maxCachedSessions: 100,
        _sessionCache: [Object] },
     Agent: { [Function: Agent] super_: [Object] },
     request: [Function: request],
     get: [Function: get] },
  agentClass:
   { [Function: Agent]
     super_: { [Function: Agent] super_: [Object], defaultMaxSockets: Infinity } },
  agent:
   Agent {
     domain: null,
     _events: { free: [Function] },
     _eventsCount: 1,
     _maxListeners: undefined,
     defaultPort: 443,
     protocol: 'https:',
     options: { path: null },
     requests: {},
     sockets: { 'hrutest.mautic.net:443:::::::::': [Array] },
     freeSockets: {},
     keepAliveMsecs: 1000,
     keepAlive: false,
     maxSockets: Infinity,
     maxFreeSockets: 256,
     maxCachedSessions: 100,
     _sessionCache: { map: [Object], list: [Array] } } }

2 个答案:

答案 0 :(得分:1)

最近几个月,我没有太多机会测试这种包装器了。

如果可能,我会尝试将其作为数组发送?

尝试这两种测试体组合:

  var testbody = [
    "name": "apitest2"
  ]

  var testbody = {[
    "name": "apitest2"
  ]}

看看结果如何?

您能否获得请求的完整日志?

答案 1 :(得分:1)

最后我解决了。至少这是一些解决方案,我不确定这是否也适用于当前功能。 我发现所有其他教程在发布数据时都使用 form 而不是 body 。此外,它们的对象不会得到JSON.stringified。

所以我的对象是

var testbody = {
    name: 'apitest99'
};

当我在node-mautic中更改功能时它起作用:

createDynamicContent: function(config, queryParameters, callback) {
    var url = config.api_endpoint + "/dynamiccontents/new?access_token=" + config.auth_object.access_token;
    //queryParameters = JSON.stringify(queryParameters);**
    request.post({
      url: url,
      form: queryParameters
    }, function(err, res) {
      if (err) {
        callback(err);
      } else {
        var asset = JSON.parse(res.body);
        callback(asset);
      }
    })
  },

我从API得到以下响应:

{ dynamicContent:
   { isPublished: true,
     dateAdded: '2018-07-13T09:37:07+00:00',
     dateModified: null,
     createdBy: 1,
     createdByUser: 'Raphael',
     modifiedBy: null,
     modifiedByUser: null,
     id: 10,
     name: 'apitest99',
     category: null,
     publishUp: null,
     publishDown: null,
     sentCount: 0,
     variantParent: null,
     variantChildren: [],
     content: null,
     filters: [],
     isCampaignBased: true,
     slotName: '' } }