Smartsheet API cURL - 将 URL 附加到工作表/行

问题描述 投票:0回答:1

我想使用 cURL API 将简单的 URL 附加到工作表或工作表中的行。 Smartsheet 文档仅说明如何附加文件。我尝试过使用请求正文并调整标头以某种方式附加 URL,但失败了,因为我不知道如何调整标头或撰写正文。

来自 Smartsheet API 文档:

将文件或 URL 附加到工作表 将文件附加到工作表。 URL 可以是以下任意一个:
普通 URL(附件类型“LINK”)
Box.com URL(附件类型“BOX_COM”)
Dropbox URL(附件类型“DROPBOX”)

curl https://api.smartsheet.com/2.0/sheets/{sheetId}/attachments \
-H "Authorization: Bearer JKlMNOpQ12RStUVwxYZAbcde3F5g6hijklM789" \
-H "Content-Type: application/msword" \
-H 'Content-Disposition: attachment; filename="ProgressReport.docx"' \
-H "Content-Length: FILE_SIZE" \
-X POST \
--data-binary @ProgressReport.docx
curl smartsheet-api
1个回答
0
投票

每当我尝试解决像您所描述的问题时,我经常发现查看我尝试创建的项目在“获取项目”API 响应中的表示方式很有帮助 - 然后我可以尝试在“创建项目”API 请求中使用相同的数据结构/内容...99% 的情况下,此方法是成功的。我将在回答您的问题时解释这个过程。

首先,我登录到 Smartsheet Web 应用程序并将 URL 附件添加到一行。

接下来,我发送了一个 List Attachments API 请求,因此当我通过 Web 应用程序添加 URL 附件时,我可以看到为 URL 附件设置的属性。

列出附件请求

GET https://api.smartsheet.com/2.0/sheets/2702602705784708/attachments

列出附件回复:

{
    "pageNumber": 1,
    "pageSize": 100,
    "totalPages": 1,
    "totalCount": 1,
    "data": [
        {
            "id": 41677365661572,
            "name": "Google",
            "url": "https://www.google.com",
            "attachmentType": "LINK",
            "parentType": "ROW",
            "parentId": 409928880836484,
            "createdAt": "2023-11-03T18:15:57Z",
            "createdBy": {
                "name": "Kim Brandl",
                "email": "*****@gmail.com"
            }
        }
    ]
}

此 API 响应中存在的属性中,只有 3 个实际上描述了 URL 附件本身:

  • name
  • URL
  • attachmentType

现在我知道为 URL 附件设置了哪些属性(通过应用程序添加),我将尝试发出一个 Attach URL API 请求,为我想要附加到工作表的 URL 设置这 3 个属性.

附加 URL(到工作表)请求:

POST https://api.smartsheet.com/2.0/sheets/2702602705784708/attachments

Authorization: Bearer API_TOKEN_VALUE_HERE
Content-Type: application/json

{
    "name": "Bing",
    "url": "https://www.bing.com",
    "attachmentType": "LINK"
}

在 cURL 中,该请求将指定如下:

curl https://api.smartsheet.com/2.0/sheets/{sheetId}/attachments \
-H "Authorization: Bearer API_TOKEN_VALUE_HERE" \
-H "Content-Type: application/json" \
-X POST \
-d '{"name": "Bing", "url": "https://www.bing.com", "attachmentType": "LINK"}'

请注意:您可能需要转义

url
参数值中的正斜杠字符。

发出此请求后,我收到以下回复:

附上URL回复:

{
    "message": "SUCCESS",
    "resultCode": 0,
    "result": {
        "id": 5022902287290244,
        "name": "Bing",
        "url": "https://www.bing.com",
        "attachmentType": "LINK",
        "parentType": "SHEET",
        "parentId": 2702602705784708,
        "createdAt": "2023-11-03T18:20:29Z",
        "createdBy": {
            "name": "Kim Brandl",
            "email": "*****@gmail.com"
        }
    },
    "version": 20
}

宾果! -- URL 附件已成功添加到工作表中。

请注意,上面的示例代码将 URL 附件添加到Sheet。要将 URL 附件添加到 Row,请求 URL 需要包含

/rows/{rowId}
,如下所示:

curl https://api.smartsheet.com/2.0/sheets/{sheetId}/rows/{rowId}/attachments \
-H "Authorization: Bearer API_TOKEN_VALUE_HERE" \
-H "Content-Type: application/json" \
-X POST \
-d '{"name": "Bing", "url": "https://www.bing.com", "attachmentType": "LINK"}'

再次请注意:您可能需要对

url
参数值中的正斜杠字符进行转义。

© www.soinside.com 2019 - 2024. All rights reserved.