Chilkat Online Tools

SQL Server / Sunshine Conversations API / Upload Attachment

Back to Collection Items

-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    -- This example assumes the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    DECLARE @http int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @success int

    EXEC sp_OASetProperty @http, 'BasicAuth', 1
    EXEC sp_OASetProperty @http, 'Login', 'username'
    EXEC sp_OASetProperty @http, 'Password', 'password'

    DECLARE @req int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.HttpRequest', @req OUT

    EXEC sp_OASetProperty @req, 'HttpVerb', 'POST'
    EXEC sp_OASetProperty @req, 'Path', '/v2/apps/{{appId}}/attachments?access=public&for=message&conversationId=c616a583e4c240a871818541'
    EXEC sp_OASetProperty @req, 'ContentType', 'multipart/form-data'
    EXEC sp_OAMethod @req, 'AddParam', NULL, 'source', 'amet officia cons'

    DECLARE @resp int
    EXEC sp_OAMethod @http, 'SynchronousRequest', @resp OUT, 'domain.com', 443, 1, @req
    EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @req
        RETURN
      END

    DECLARE @sbResponseBody int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.StringBuilder', @sbResponseBody OUT

    EXEC sp_OAMethod @resp, 'GetBodySb', @success OUT, @sbResponseBody

    DECLARE @jResp int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.JsonObject', @jResp OUT

    EXEC sp_OAMethod @jResp, 'LoadSb', @success OUT, @sbResponseBody
    EXEC sp_OASetProperty @jResp, 'EmitCompact', 0


    PRINT 'Response Body:'
    EXEC sp_OAMethod @jResp, 'Emit', @sTmp0 OUT
    PRINT @sTmp0

    DECLARE @respStatusCode int
    EXEC sp_OAGetProperty @resp, 'StatusCode', @respStatusCode OUT

    PRINT 'Response Status Code = ' + @respStatusCode
    IF @respStatusCode >= 400
      BEGIN

        PRINT 'Response Header:'
        EXEC sp_OAGetProperty @resp, 'Header', @sTmp0 OUT
        PRINT @sTmp0

        PRINT 'Failed.'
        EXEC @hr = sp_OADestroy @resp

        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @req
        EXEC @hr = sp_OADestroy @sbResponseBody
        EXEC @hr = sp_OADestroy @jResp
        RETURN
      END
    EXEC @hr = sp_OADestroy @resp

    -- Sample JSON response:
    -- (Sample code for parsing the JSON response is shown below)

    -- {
    --   "attachment": {
    --     "mediaUrl": "http://smooch.io/rocks.smooch.media-dev/apps/5ec41c54fe13cc5ac404bedc/conversations/c616a583e4c240a871818541/TmYMVQUBNsQRItX4fKf4aC-T/Screen%20Shot%202020-09-02%20at%204.04.41%20PM.png",
    --     "mediaType": "image/png"
    --   }
    -- }

    -- Sample code for parsing the JSON response...
    -- Use this online tool to generate parsing code from sample JSON: Generate JSON Parsing Code

    DECLARE @MediaUrl nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @MediaUrl OUT, 'attachment.mediaUrl'
    DECLARE @MediaType nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @MediaType OUT, 'attachment.mediaType'

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @req
    EXEC @hr = sp_OADestroy @sbResponseBody
    EXEC @hr = sp_OADestroy @jResp


END
GO

Curl Command

curl -X POST
	-u 'username:password'
	-H "Content-Type: multipart/form-data"
	--form 'source=amet officia cons'
https://domain.com/v2/apps/{{appId}}/attachments?access=public&for=message&conversationId=c616a583e4c240a871818541

Postman Collection Item JSON

{
  "name": "Upload Attachment",
  "_postman_id": "7ce74c14-487b-4d62-aeb1-7d1ab9d98568",
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "value": "multipart/form-data"
      }
    ],
    "body": {
      "mode": "formdata",
      "formdata": [
        {
          "key": "source",
          "value": "amet officia cons",
          "type": "text"
        }
      ]
    },
    "url": {
      "raw": "{{url}}/v2/apps/{{appId}}/attachments?access=public&for=message&conversationId=c616a583e4c240a871818541",
      "host": [
        "{{url}}"
      ],
      "path": [
        "v2",
        "apps",
        "{{appId}}",
        "attachments"
      ],
      "query": [
        {
          "key": "access",
          "value": "public",
          "description": "(Required) The access level for the attachment. Currently the available access levels are public and private."
        },
        {
          "key": "for",
          "value": "message",
          "description": "Specifies the intended container for the attachment, to enable automatic attachment deletion (on deletion of associated message, conversation or user). For now, only message is supported. See [Attachments for Messages](#section/Attachments-for-Messages) for details."
        },
        {
          "key": "conversationId",
          "value": "c616a583e4c240a871818541",
          "description": "Links the attachment getting uploaded to the conversation ID."
        }
      ]
    },
    "description": "Upload an attachment to Sunshine Conversations to use in future messages. Files are uploaded using the multipart/form-data content type. Use the returned mediaUrl to send a file, image or carousel message.\n<aside class=\"notice\">Note that Sunshine Conversations limits the size and type of file you can upload to the platform. See the <a href=\"https://docs.smooch.io/guide/validating-files\">file validation</a> guide for more details.</aside>\n"
  },
  "response": [
    {
      "id": "7fd2235e-ca63-4848-b2cd-9e12ac4c8d35",
      "name": "Bad request",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "description": {
              "content": "Added as a part of security scheme: bearer",
              "type": "text/plain"
            },
            "key": "Authorization",
            "value": "Bearer <token>"
          }
        ],
        "body": {
          "mode": "formdata",
          "formdata": [
            {
              "key": "source",
              "value": "amet officia cons",
              "description": {
                "content": "",
                "type": "text/plain"
              },
              "type": "text"
            }
          ]
        },
        "url": {
          "raw": "{{baseUrl}}/v2/apps/:appId/attachments?access=public&for=message&conversationId=c616a583e4c240a871818541",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "v2",
            "apps",
            ":appId",
            "attachments"
          ],
          "query": [
            {
              "key": "access",
              "value": "public"
            },
            {
              "key": "for",
              "value": "message"
            },
            {
              "key": "conversationId",
              "value": "c616a583e4c240a871818541"
            }
          ],
          "variable": [
            {
              "key": "appId"
            }
          ]
        }
      },
      "status": "Bad Request",
      "code": 400,
      "_postman_previewlanguage": "text",
      "header": [
        {
          "key": "Content-Type",
          "value": "text/plain"
        }
      ],
      "cookie": [
      ],
      "body": ""
    },
    {
      "id": "e96c5e0e-c349-4158-8835-ec9268391da7",
      "name": "Created",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "description": {
              "content": "Added as a part of security scheme: bearer",
              "type": "text/plain"
            },
            "key": "Authorization",
            "value": "Bearer <token>"
          }
        ],
        "body": {
          "mode": "formdata",
          "formdata": [
            {
              "key": "source",
              "value": "amet officia cons",
              "description": {
                "content": "",
                "type": "text/plain"
              },
              "type": "text"
            }
          ]
        },
        "url": {
          "raw": "{{baseUrl}}/v2/apps/:appId/attachments?access=public&for=message&conversationId=c616a583e4c240a871818541",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "v2",
            "apps",
            ":appId",
            "attachments"
          ],
          "query": [
            {
              "key": "access",
              "value": "public"
            },
            {
              "key": "for",
              "value": "message"
            },
            {
              "key": "conversationId",
              "value": "c616a583e4c240a871818541"
            }
          ],
          "variable": [
            {
              "key": "appId"
            }
          ]
        }
      },
      "status": "Created",
      "code": 201,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n \"attachment\": {\n  \"mediaUrl\": \"http://smooch.io/rocks.smooch.media-dev/apps/5ec41c54fe13cc5ac404bedc/conversations/c616a583e4c240a871818541/TmYMVQUBNsQRItX4fKf4aC-T/Screen%20Shot%202020-09-02%20at%204.04.41%20PM.png\",\n  \"mediaType\": \"image/png\"\n }\n}"
    }
  ]
}