node.jsmautic

Mautic API cannot create DynamicContent


I am struggling to successfully execute create calls on the mautic REST api. In my node project I am using a standard express installation and a wrapper for the node api https://github.com/sambarnes90/node-mautic

I have authenticated my machine with the api and can PULL data without problem. The problem is POSTing data to it.

The documentation for the targeted post request is here: https://developer.mautic.org/#create-dynamic-content

This is my route. It derives from the example in node-mautic which works fine, but putting something into mautic does not work for me:

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);
        });
      });*/
    }
  });
});

I am getting this response from the API:

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

This is the GitHub project for this: https://github.com/raphaelurban/mautic-api-test

EDIT: I also tried:

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

with the same result.

Here is the full POST request:

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] } } }

Solution

  • Finally I worked it out. At least it is some solution, I am not sure if this also works with the current function. I found that all other tutorials use form instead of body when posting data. Additionally, their objects do not get JSON.stringified.

    So with my object:

    var testbody = {
        name: 'apitest99'
    };
    

    It works when I change the function in 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);
          }
        })
      },
    

    And I am getting the following response from the 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: '' } }