Mautic API 无法创建 DynamicContent

Mautic API cannot create DynamicContent

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

我已经使用 api 验证了我的机器,并且可以毫无问题地提取数据。问题是 POST 向它发送数据。

目标 post 请求的文档在此处:https://developer.mautic.org/#create-dynamic-content

这是我的路线。它源自 node-mautic 中的示例,它工作正常,但将某些东西放入 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] } } }

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

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

尝试这两种测试体组合:

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

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

看看你得到了什么结果?

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

我终于搞定了。至少它是一些解决方案,我不确定这是否也适用于当前功能。 我发现所有其他教程在发布数据时都使用 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: '' } }