Freshdesk API 创建带有附件抛出验证错误的笔记

Freshdesk API Create note with attachment throwing validation error

use GuzzleHttp\Client;

function insert_freshdesk_note($ticketId, $content, $attachments=null)
{
    if(is_null($content)) {
        return false;
    }

    $url = 'https://mydomain.freshdesk.com/api/v2/tickets/'.$ticketId.'/notes';
    $method = 'POST';
    $userName = config('freshdesk_api_key');
    $password = 'password';

    $data = (!empty($attachments)) ? [
        "attachments[]" => $attachments,
        "body" => $content,
        "private" => false,
    ] : [
        "body" => $content,
        "private" => false,
    ];

    $options = (!empty($attachments)) ? [
        'json' => $data,
        'auth' => [$userName, $password],
        'headers' => ['content-type' => 'multipart/form-data']
    ] : [
        'json' => $data,
        'auth' => [$userName, $password]
    ];

    $client = new Client();

    try {
        $response = $client->request($method, $url, $options);
        return json_decode($response->getBody(), true);
    } catch (Exception $e) {
        Log::error($e);
    }
}

上面的代码在没有附件的情况下工作正常,但是当附件进入图片时它会抛出以下错误:-

GuzzleHttp\Exception\ClientException: Client error: `POST https://mydomain.freshdesk.com/api/v2/tickets/13033/notes` resulted in a `400 Bad Request` response:
{"description":"Validation failed","errors":[{"field":"{\"attachments","message":"Unexpected/invalid field in request","

我正在根据文档进行操作,但此时我已经走到了尽头。我尝试了其他排列和组合,但通过这些,我无法解决这个问题。

谁能帮帮我。 这是 freshdesk 文档的 link 在 $attachments[] = '@/path/to/xyz.ext' 中,这个特别的是。

函数调用将是这样的:-

insert_freshdesk_note($this->freshdesk_ticket_id, $noteText, $image->image_full_path);

在上面的问题中,我尝试使用GuzzleHTTP 客户端、Laravel 框架和PHP 语言来实现注释的添加。这是它开始工作的以下源代码。

use GuzzleHttp\Client;

function insert_freshdesk_note($ticketId, $content, $attachments=null)
{   
    if(is_null($content)) {
        return false;
    }

    $url = 'https://mydomain.freshdesk.com/api/v2/tickets/'.$ticketId.'/notes';
    $method = 'POST';
    $userName = config('freshdesk_api_key');
    $password = 'password';

    $attachmentsFilePath = explode('/',$attachments);
    $fileName = end($attachmentsFilePath);

    $options = (!empty($attachments)) ? [
        'multipart' => [
            [
                'name'     => 'body',
                'contents' => $content,
            ],
            [
                'name'     => 'private',
                'contents' => "false",
            ],
            [
                'name'     => 'attachments[]',
                'contents' => fopen($attachments, 'r'),
                'filename' => $fileName,
            ],
        ],
        'auth' => [$userName, $password],
    ] : [
        'json' => [
            "body" => $content,
            "private" => false,
        ],
        'auth' => [$userName, $password]
    ];

    $client = new Client();

    try {
        $response = $client->request($method, $url, $options);
        return json_decode($response->getBody(), true);
    } catch (Exception $e) {
        Log::error($e);
    }
}

我们必须以多部分方式发送数据,以便 Freshdesk 后端了解数据在那里,在 header 中写入 multipart/form-type 无济于事。如果您还查看带有和不带附件的文档,请查看以下附件:-

没有附件:-

curl -v -u user@yourcompany.com:test -H "Content-Type: application/json" -X POST -d '{ "body":"Hi tom, Still Angry", "private":false, "notify_emails":["tom@yourcompany.com"] }' 'https://domain.freshdesk.com/api/v2/tickets/3/notes'

附附件:-

curl -v -u user@yourcompany.com:test -F "attachments[]=@/path/to/attachment1.ext" -F "body=Hi tom, Still Angry" -F "notify_emails[]=tom@yourcompany.com" -X POST 'https://domain.freshdesk.com/api/v2/tickets/20/notes'

可以看出卷曲的不同。 这是我忽略的缺失部分。