Pipedrive API对过滤器端点的PUT和POST请求不工作[英] Pipedrive API PUT and POST requests for Filters endpoint not working

本文是小编为大家收集整理的关于Pipedrive API对过滤器端点的PUT和POST请求不工作的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在尝试从PipEdrive API进行特定的日期范围.我通过通过API编辑(或创建/删除)滤波器来完成此目的,然后在后续请求中通过该过滤器来拉动所有交易.

"交易"端点完美地工作.我的问题是,API似乎没有喜欢我通过"过滤器"端点的"条件"参数 - 但只有我正在使用curl(在我自己的代码中)或邮递员.如果我在API文档中测试"编辑过滤器"或"创建过滤器"端点,则它们都按照我将JSON对象从我的代码复制并粘贴到"条件"参数时完全按预期工作.但是,如果我使用卷曲或邮递员,Put Endpoint只是返回我在不编辑它的情况下返回我编辑的过滤器,并且后端点创建一个带有空条件的新过滤器.

这是我使用的php代码,我正在使用post端点:

$data = [
    'name' => 'Custom date range',
    'type' => 'deals',
    'conditions' => '{
        "glue": "and",
        "conditions": [
            {
                "glue": "and",
                "conditions": [
                    {
                    "object": "deal",
                    "field_id": "12449",
                    "operator": "=",
                    "value": "won",
                    "extra_value": "null"
                    },
                    {
                      "object": "deal",
                    "field_id": "12455",
                    "operator": ">=",
                    "value": "2017-03-01",
                    "extra_value": "null"
                    },
                    {
                      "object": "deal",
                    "field_id": "12455",
                    "operator": "<=",
                    "value": "2017-03-10",
                    "extra_value": "null"
                    }
                ]
            },
            {
                "glue": "or",
                "conditions": []
            }
        ]
     }'
];

$ch = curl_init("https://api.pipedrive.com/v1/filters?api_token=$apiKey");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/json;"));
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));
$response = curl_exec($ch);

,这是"创建筛选器"端点的"条件"参数的描述:

"过滤条件作为JSON对象.它需要最小的结构如下:

{"glue":"and","conditions":[{"glue":"and","conditions": [CONDITION_OBJECTS]},{"glue":"or","conditions":[CONDITION_OBJECTS]}]}

用以下结构的JSON对象替换条件_Objects:

{"object":"","field_id":"", "operator":"","value":"", "extra_value":""} or leave the array empty. 

取决于对象类型,您应该使用另一个API端点来获取Field_ID.您可以选择五种类型的对象:

"person", "deal", "organization", "product", "activity" 

,您可以使用这些类型的运算符,具体取决于您的字段的类型:

"IS NOT NULL", "IS NULL", "<=", ">=", "<", ">", "!=", "=", "LIKE '%$%'", "NOT LIKE '%$%'", "LIKE '$%'", "NOT LIKE '$%'", "LIKE '%$'", "NOT LIKE '%$'".

更好地了解过滤器工作如何尝试直接从PipEdrive应用程序创建它们."

后端点的"条件"参数是相同的.同样,当我将那只大JSON对象粘贴到API Docs测试时,两个端点都完美地工作 - 但不是在我自己的代码中.任何帮助都会受到赞赏.

编辑:这是我从CURL获取"创建过滤器"端点的响应:

{#233 ▼
    +"id": 60
    +"name": "Custom date range"
    +"active_flag": true
    +"type": "deals"
    +"temporary_flag": null
    +"user_id": 504569
    +"add_time": "2017-04-19 11:18:10"
    +"update_time": "2017-04-19 11:18:10"
    +"visible_to": "7"
    +"custom_view_id": null
    +"conditions": {#219 ▼
        +"glue": "and"
        +"conditions": array:2 [▼
            0 => {#230 ▼
                +"glue": "and"
                +"conditions": []
            }
            1 => {#223 ▼
                +"glue": "or"
                +"conditions": []
            }
        ]
    }
}

没有错误,但正如您所看到的,条件是空的.我也刚刚尝试为PipEdrive API创建的PHP包装器,并获得相同的结果.

推荐答案

Pipedrive工程师在这里.这是一个我测试的一个例子..

<?php
$data = '
{
    "name":"Custom filter less than 1000",
    "type":"deals",
    "visible_to":1,
    "conditions":{
        "glue": "and",
        "conditions":[
            {
                "glue": "and",
                "conditions": [
                        {
                            "object": "deal",
                            "field_id": "12452",
                            "operator": "<",
                            "value": 1000,
                            "extra_value": null
                        }
                    ]
                },
            {
                "glue": "or",
                "conditions": []
            }
        ]
    }
}
';

$ch = curl_init("https://api.pipedrive.com/v1/filters?api_token=xxx");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json;', 'Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POST,           1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);

echo $response;

注意:

  1. 整个JSON是Rraw-Posted
  2. 我添加了内容类型标题
  3. 我添加了visible_to param

过滤API非常复杂,我们正在努力改进文档.希望这有助于

其他推荐答案

拉动交易,您应该使用GET方法. POST用于添加交易.

从POST更改CURL到GET方法,它应该工作.

本文地址:https://www.itbaoku.cn/post/1937793.html

问题描述

I'm trying to pull Deals from the Pipedrive API for a specific date range. I'm accomplishing this by editing (or creating/deleting) a filter via the API with the date range I want, and then passing in that filter in a subsequent request to pull all deals.

The "deals" endpoint works perfectly. My problem is that the API doesn't seem to like the "conditions" parameter that I'm passing in for the "filters" endpoint--but only if I'm using cURL (in my own code) or Postman. If I test either the "edit filter" or "create filter" endpoints in the API docs, both of them work exactly as intended when I copy and paste the JSON object from my code into the "conditions" parameter. However, if I use cURL or Postman, the PUT endpoint simply returns the filter I'm editing without editing it, and the POST endpoint creates a new filter with empty conditions.

Here's the PHP code I'm using for the POST endpoint:

$data = [
    'name' => 'Custom date range',
    'type' => 'deals',
    'conditions' => '{
        "glue": "and",
        "conditions": [
            {
                "glue": "and",
                "conditions": [
                    {
                    "object": "deal",
                    "field_id": "12449",
                    "operator": "=",
                    "value": "won",
                    "extra_value": "null"
                    },
                    {
                      "object": "deal",
                    "field_id": "12455",
                    "operator": ">=",
                    "value": "2017-03-01",
                    "extra_value": "null"
                    },
                    {
                      "object": "deal",
                    "field_id": "12455",
                    "operator": "<=",
                    "value": "2017-03-10",
                    "extra_value": "null"
                    }
                ]
            },
            {
                "glue": "or",
                "conditions": []
            }
        ]
     }'
];

$ch = curl_init("https://api.pipedrive.com/v1/filters?api_token=$apiKey");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/json;"));
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));
$response = curl_exec($ch);

And this is the description of the "conditions" parameter for the "create filter" endpoint:

"Filter conditions as a JSON object. It requires a minimum structure as follows:

{"glue":"and","conditions":[{"glue":"and","conditions": [CONDITION_OBJECTS]},{"glue":"or","conditions":[CONDITION_OBJECTS]}]}

Replace CONDITION_OBJECTS with JSON objects of the following structure:

{"object":"","field_id":"", "operator":"","value":"", "extra_value":""} or leave the array empty. 

Depending on the object type you should use another API endpoint to get field_id. There are five types of objects you can choose from:

"person", "deal", "organization", "product", "activity" 

and you can use these types of operators depending on what type of a field you have:

"IS NOT NULL", "IS NULL", "<=", ">=", "<", ">", "!=", "=", "LIKE '%$%'", "NOT LIKE '%$%'", "LIKE '$%'", "NOT LIKE '$%'", "LIKE '%$'", "NOT LIKE '%$'".

To get a better understanding of how filters work try creating them directly from the Pipedrive application."

The POST endpoint's "conditions" parameter is the same. Again, when I paste that big JSON object into the API docs test, both endpoints works perfectly--but not in my own code. Any help would be appreciated.

Edit: this is the response I get from cURL for the "create filter" endpoint:

{#233 ▼
    +"id": 60
    +"name": "Custom date range"
    +"active_flag": true
    +"type": "deals"
    +"temporary_flag": null
    +"user_id": 504569
    +"add_time": "2017-04-19 11:18:10"
    +"update_time": "2017-04-19 11:18:10"
    +"visible_to": "7"
    +"custom_view_id": null
    +"conditions": {#219 ▼
        +"glue": "and"
        +"conditions": array:2 [▼
            0 => {#230 ▼
                +"glue": "and"
                +"conditions": []
            }
            1 => {#223 ▼
                +"glue": "or"
                +"conditions": []
            }
        ]
    }
}

No errors, but as you can see, the conditions are empty. I also just tried the PHP wrapper that was created for the Pipedrive API and am getting the same result.

推荐答案

Pipedrive engineer here. Here is an example I tested..

<?php
$data = '
{
    "name":"Custom filter less than 1000",
    "type":"deals",
    "visible_to":1,
    "conditions":{
        "glue": "and",
        "conditions":[
            {
                "glue": "and",
                "conditions": [
                        {
                            "object": "deal",
                            "field_id": "12452",
                            "operator": "<",
                            "value": 1000,
                            "extra_value": null
                        }
                    ]
                },
            {
                "glue": "or",
                "conditions": []
            }
        ]
    }
}
';

$ch = curl_init("https://api.pipedrive.com/v1/filters?api_token=xxx");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json;', 'Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POST,           1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);

echo $response;

Notice that

  1. Entire json is raw-posted
  2. I added Content-Type header
  3. I added visible_to param

Filtering API is quite complex and we are working on improving documentation. Hope this helps

其他推荐答案

To Pull deals, you should be using GET method. POST is used to add deals.

Change CURL from POST to GET method and it should work.