Mautic can not create segment with bounce filter

125 views Asked by At

I am trying to create segment in mautic for bounced emails, but I am having trouble with filters, code works correctly and segment is created when I comment out filters, but I need to have filters as well. Currently I get error:

{"errors":[{"code":400,"message":"properties: This form should not contain extra fields., This form should not contain extra fields., operator: This value is not valid.","details":{"properties":["This form should not contain extra fields."],"0":["This form should not contain extra fields."],"operator":["This value is not valid."]}}]} An error occurred: Request failed with status 400

Here is code:

bounce_filters = [
    {
        "glue": "and",
        "field": "bounced_email",
        "type": "boolean",
        "operator": "=",
        "value": "1",
    }
]


 create_segment(
     'username',
     'password',
     segment_name="Bounced emails",
     filters=bounce_filters,
     alias="bounced_emails",
     is_published=1,
     is_global=0
 )


def create_segment(self, username, password, segment_name, filters, alias=None, is_published=1, is_global=0):
    """Create a new segment in Mautic using Basic Authentication."""

    # API endpoint
    url = f"https://{domain}/api/segments/new"

    print("API endpoint url: " + url)

    # API parameters
    data = {
        "name": segment_name,
        "alias": alias if alias else segment_name.lower().replace(" ", "_"),
        "isPublished": is_published,
        "isGlobal": is_global,
        "filters": filters
    }

    # Adding headers
    headers = {'Content-Type': 'application/json'}

    # Making a POST request with Basic Authentication
    response = requests.post(url, data=json.dumps(data), headers=headers, auth=(username, password))


    print(response.text)

    # Error handling
    if response.status_code != 200:
        raise Exception(f"Request failed with status {response.status_code}")

    # Check the response for success
    print(f'Response Text: {response.text}')
    if 'application/json' in response.headers['Content-Type']:
        result = json.loads(response.text)

        if 'errors' in result:
            raise Exception("Failed to create segment: " + result['errors'][0]['message'])

        print(f"Segment {segment_name} created successfully.")

    else:
        print('Response is not in JSON format.')

Here is API documentation: CREATE SEGMENT

<?php

$data = array(
    'name'        => 'Segment A',
    'alias'       => 'segment-a',
    'description' => 'This is my first segment created via API.',
    'isPublished' => 1,
    'filters' => array(
        array(
            'glue' => 'and',
            'field' => 'email',
            'object' => 'lead',
            'type' => 'email',
            'filter' => '*@gmail.com',
            'operator' => 'like',
        ),
    ),
);
$segment = $segmentApi->create($data);
Create a new segment.

HTTP Request
POST /segments/new

Post Parameters

Name    Description
name    Segment name is the only required field
alias   Name alias generated automatically if not set
description A description of the segment.
isPublished A value of 0 or 1
isGlobal    boolean
filters array
Segment Filter Properties

Name    Type    Description
glue    string  How to glue the filters to others. Possible values: and, or
field   string  Alias of the contact or company field to based the filter on
object  string  Object which have the field. Possible values: 'lead’ (for contacts), company
type    string  Type of the field. Possible values: 'boolean’, date (format Y-m-d), datetime (format Y-m-d H:i:s), email, country, locale, lookup, number, tel, region, select, multiselect, text, textarea, time, timezone, url
operator    string  Operator used for matching the values. Possible values: ’=’, !=, empty, !empty, like, !like, regexp, !regexp, startsWith, endsWith, contains
Response
Expected Response Code: 201

Properties

Same as Get Segment.
0

There are 0 answers