Chilkat Online Tools

SQL Server / Plivo REST API / Upload Media

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
    -- Use "Chilkat_9_5_0.Http" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.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', '{{auth_id}}'
    EXEC sp_OASetProperty @http, 'Password', 'password'

    DECLARE @req int
    -- Use "Chilkat_9_5_0.HttpRequest" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.HttpRequest', @req OUT

    EXEC sp_OASetProperty @req, 'HttpVerb', 'POST'
    EXEC sp_OASetProperty @req, 'Path', '/v1/Account/<auth_id>/Media/'
    EXEC sp_OASetProperty @req, 'ContentType', 'multipart/form-data'
    EXEC sp_OAMethod @req, 'AddFileForUpload2', @success OUT, 'file', ' path to file', 'application/octet-stream'

    EXEC sp_OAMethod @req, 'AddHeader', NULL, 'Expect', '100-continue'

    DECLARE @resp int
    EXEC sp_OAMethod @http, 'SynchronousRequest', @resp OUT, 'api.plivo.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

    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
    PRINT @iTmp0
    EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
    PRINT @sTmp0
    EXEC @hr = sp_OADestroy @resp


    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @req


END
GO

Curl Command

curl -X POST
	-u '{{auth_id}}:password'
	--form 'file=@"/path/to/file"'
https://api.plivo.com/v1/Account/<auth_id>/Media/

Postman Collection Item JSON

{
  "name": "Upload Media",
  "request": {
    "method": "POST",
    "header": [
    ],
    "body": {
      "mode": "formdata",
      "formdata": [
        {
          "key": "file",
          "type": "file",
          "src": "/Users/mohammedhuzaif/Downloads/image.png"
        }
      ]
    },
    "url": {
      "raw": "https://api.plivo.com/v1/Account/{{auth_id}}/Media/",
      "protocol": "https",
      "host": [
        "api",
        "plivo",
        "com"
      ],
      "path": [
        "v1",
        "Account",
        "{{auth_id}}",
        "Media",
        ""
      ]
    },
    "description": "This API lets you upload media files which can be used to send MMS. Plivo supports up to a total of 10 attachments in an upload API/SDK call with a maximum of 2MB per attachment. \nMore information can be found [here](https://www.plivo.com/docs/sms/api/media#upload-media)\n\n| Arguments   | Description | Required/Conditional/Optional     |\n| :---        |    :----:   |          ---: |\n| file      | This argument allows you to attach one or more(max 10) files as attachments in the body of the request.| Required |"
  },
  "response": [
    {
      "name": "Upload Media",
      "originalRequest": {
        "method": "POST",
        "header": [
        ],
        "body": {
          "mode": "formdata",
          "formdata": [
            {
              "key": "file",
              "type": "file",
              "src": "/Users/mohammedhuzaif/Downloads/image.png"
            }
          ]
        },
        "url": {
          "raw": "https://api.plivo.com/v1/Account/{{auth_id}}/Media/",
          "protocol": "https",
          "host": [
            "api",
            "plivo",
            "com"
          ],
          "path": [
            "v1",
            "Account",
            "{{auth_id}}",
            "Media",
            ""
          ]
        }
      },
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
      ],
      "cookie": [
      ],
      "body": "{\n    \"api_id\": \"<api_id>\",\n    \"objects\": [\n        {\n            \"content_type\": \"image/jpeg\",\n            \"file_name\": \"sample_file1.jpg\",\n            \"media_id\": \"<media_id>\",\n            \"size\": 85277,\n            \"status\": \"success\",\n            \"status_code\": 201,\n            \"upload_time\": \"2020-02-17T07:16:09.153289Z\",\n            \"media_url\": \"https://media.plivo.com/Account/<auth_id>/Media/<media_id>\"\n        },\n        {\n            \"content_type\": \"image/png\",\n            \"file_name\": \"sample_file2.png\",\n            \"media_id\": \"<media_id>\",\n            \"size\": 16709,\n            \"status\": \"success\",\n            \"status_code\": 201,\n            \"upload_time\": \"2020-02-18T11:21:55.972100055Z\",\n            \"media_url\": \"https://media.plivo.com/Account/<auth_id>/Media/<media_id>\"\n        }\n    ]\n}"
    }
  ]
}