Chilkat Online Tools

Python / New FreshBooks / Upload Logo or Proposal Image

Back to Collection Items

import sys
import chilkat2

# This example assumes the Chilkat API to have been previously unlocked.
# See Global Unlock Sample for sample code.

http = chilkat2.Http()

req = chilkat2.HttpRequest()
req.HttpVerb = "POST"
req.Path = "/uploads/account/{{accountId}}/images"
req.ContentType = "multipart/form-data"
success = req.AddFileForUpload2("content"," path to file","application/octet-stream")

req.AddHeader("Authorization","Bearer <access_token>")
req.AddHeader("Expect","100-continue")

# resp is a CkHttpResponse
resp = http.SynchronousRequest("api.freshbooks.com",443,True,req)
if (http.LastMethodSuccess == False):
    print(http.LastErrorText)
    sys.exit()

sbResponseBody = chilkat2.StringBuilder()
resp.GetBodySb(sbResponseBody)

jResp = chilkat2.JsonObject()
jResp.LoadSb(sbResponseBody)
jResp.EmitCompact = False

print("Response Body:")
print(jResp.Emit())

respStatusCode = resp.StatusCode
print("Response Status Code = " + str(respStatusCode))
if (respStatusCode >= 400):
    print("Response Header:")
    print(resp.Header)
    print("Failed.")

    sys.exit()

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

# {
#   "image": {
#     "jwt": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhY2NvdW50Ijo0MjM2NDEwLCJvcmlnaW5hbF9maWxlbmFtZSI6InRvcm9udG8tc2t5bGluZS5qcGciLCJidWNrZXQiOiJ1cGxvYWRzIiwiZmlsZW5hbWUiOiJ1cGxvYWQtOTE5ODE2MGY5NGNhMjk3ZTE3NGE2MDJmNDA4OTlhM2M5Zjc2MDk5ZCIsImxlbmd0aCI6MzA3MDY4LCJrZXkiOiInZG9jcy0nLTQyMzY0MTAvdXBsb2FkLTkxOTgxNjBmOTRjYTI5N2UxNzRhNjAyZjQwODk5YTNjOWY3NjA5OWQifQ.yAfSd2cIAf2GONitgBmimMCHxvH9OYij7g0WMsiLsaE",
#     "public_id": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhY2NvdW50Ijo0MjM2NDEwLCJvcmlnaW5hbF9maWxlbmFtZSI6InRvcm9udG8tc2t5bGluZS5qcGciLCJidWNrZXQiOiJ1cGxvYWRzIiwiZmlsZW5hbWUiOiJ1cGxvYWQtOTE5ODE2MGY5NGNhMjk3ZTE3NGE2MDJmNDA4OTlhM2M5Zjc2MDk5ZCIsImxlbmd0aCI6MzA3MDY4LCJrZXkiOiInZG9jcy0nLTQyMzY0MTAvdXBsb2FkLTkxOTgxNjBmOTRjYTI5N2UxNzRhNjAyZjQwODk5YTNjOWY3NjA5OWQifQ.yAfSd2cIAf2GONitgBmimMCHxvH9OYij7g0WMsiLsaE",
#     "filename": "upload-9198160f94ca297e174a602f40899a3c9f76099d"
#   },
#   "link": "https://my.freshbooks.com/service/uploads/images/eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhY2NvdW50Ijo0MjM2NDEwLCJvcmlnaW5hbF9maWxlbmFtZSI6InRvcm9udG8tc2t5bGluZS5qcGciLCJidWNrZXQiOiJ1cGxvYWRzIiwiZmlsZW5hbWUiOiJ1cGxvYWQtOTE5ODE2MGY5NGNhMjk3ZTE3NGE2MDJmNDA4OTlhM2M5Zjc2MDk5ZCIsImxlbmd0aCI6MzA3MDY4LCJrZXkiOiInZG9jcy0nLTQyMzY0MTAvdXBsb2FkLTkxOTgxNjBmOTRjYTI5N2UxNzRhNjAyZjQwODk5YTNjOWY3NjA5OWQifQ.yAfSd2cIAf2GONitgBmimMCHxvH9OYij7g0WMsiLsaE"
# }

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

v_Jwt = jResp.StringOf("image.jwt")
Public_id = jResp.StringOf("image.public_id")
Filename = jResp.StringOf("image.filename")
link = jResp.StringOf("link")

Curl Command

curl -X POST
	-H "Authorization: Bearer <access_token>"
	-H "Content-Type: multipart/form-data"
	--form 'content=@"/path/to/file"'
https://api.freshbooks.com/uploads/account/{{accountId}}/images

Postman Collection Item JSON

{
  "name": "Upload Logo or Proposal Image",
  "event": [
    {
      "listen": "test",
      "script": {
        "exec": [
          "let jsonData = JSON.parse(responseBody); ",
          "pm.environment.set(\"uploadImg\", \"\\'\" + jsonData.image.jwt + \"\\\"\")"
        ],
        "type": "text/javascript"
      }
    }
  ],
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "name": "Content-Type",
        "type": "text",
        "value": "multipart/form-data",
        "disabled": true
      }
    ],
    "body": {
      "mode": "formdata",
      "formdata": [
        {
          "key": "content",
          "type": "file",
          "src": [
          ]
        }
      ]
    },
    "url": {
      "raw": "https://api.freshbooks.com/uploads/account/{{accountId}}/images",
      "protocol": "https",
      "host": [
        "api",
        "freshbooks",
        "com"
      ],
      "path": [
        "uploads",
        "account",
        "{{accountId}}",
        "images"
      ]
    }
  },
  "response": [
    {
      "name": "Upload Logo",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "name": "Content-Type",
            "value": "multipart/form-data",
            "type": "text",
            "disabled": true
          }
        ],
        "body": {
          "mode": "formdata",
          "formdata": [
            {
              "key": "content",
              "type": "file",
              "src": [
              ]
            }
          ]
        },
        "url": {
          "raw": "https://api.freshbooks.com/uploads/account/{{accountId}}/images",
          "protocol": "https",
          "host": [
            "api",
            "freshbooks",
            "com"
          ],
          "path": [
            "uploads",
            "account",
            "{{accountId}}",
            "images"
          ]
        }
      },
      "status": "CREATED",
      "code": 201,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Server",
          "value": "nginx"
        },
        {
          "key": "Content-Type",
          "value": "application/vnd.api+json"
        },
        {
          "key": "X-Version",
          "value": "1902.2.0"
        },
        {
          "key": "X-NewRelic-App-Data",
          "value": "PxQBWV5TCBABVllRBQIEU1UTGhE1AwE2QgNWEVlbQFtcCxYnRA9QFg1ZWU4TE1RcUQATTEVURAkRQ1VWEUgKD1EFXRECUVlTVABKE1QSXBcWVVJDSlMUAh5UV1AGAwNfUgYDBFdWVFECTgtSDwoMCh9HX1IJVAIGUlIHVFQHXAoJBUNOUVBbFQFs"
        },
        {
          "key": "Access-Control-Allow-Origin",
          "value": "*"
        },
        {
          "key": "Expires",
          "value": "Tue, 24 Apr 2018 17:11:52 GMT"
        },
        {
          "key": "Cache-Control",
          "value": "no-cache"
        },
        {
          "key": "Via",
          "value": "1.1 google"
        },
        {
          "key": "Via",
          "value": "1.1 varnish"
        },
        {
          "key": "Content-Length",
          "value": "1324"
        },
        {
          "key": "Accept-Ranges",
          "value": "bytes"
        },
        {
          "key": "Date",
          "value": "Wed, 24 Apr 2019 17:11:52 GMT"
        },
        {
          "key": "Connection",
          "value": "keep-alive"
        },
        {
          "key": "X-Served-By",
          "value": "cache-mdw17356-MDW"
        },
        {
          "key": "X-Cache",
          "value": "MISS"
        },
        {
          "key": "X-Cache-Hits",
          "value": "0"
        },
        {
          "key": "Country",
          "value": "CA"
        },
        {
          "key": "Strict-Transport-Security",
          "value": "max-age=31536000; includeSubDomains; preload"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"image\": {\n        \"jwt\": \"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhY2NvdW50Ijo0MjM2NDEwLCJvcmlnaW5hbF9maWxlbmFtZSI6InRvcm9udG8tc2t5bGluZS5qcGciLCJidWNrZXQiOiJ1cGxvYWRzIiwiZmlsZW5hbWUiOiJ1cGxvYWQtOTE5ODE2MGY5NGNhMjk3ZTE3NGE2MDJmNDA4OTlhM2M5Zjc2MDk5ZCIsImxlbmd0aCI6MzA3MDY4LCJrZXkiOiInZG9jcy0nLTQyMzY0MTAvdXBsb2FkLTkxOTgxNjBmOTRjYTI5N2UxNzRhNjAyZjQwODk5YTNjOWY3NjA5OWQifQ.yAfSd2cIAf2GONitgBmimMCHxvH9OYij7g0WMsiLsaE\",\n        \"public_id\": \"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhY2NvdW50Ijo0MjM2NDEwLCJvcmlnaW5hbF9maWxlbmFtZSI6InRvcm9udG8tc2t5bGluZS5qcGciLCJidWNrZXQiOiJ1cGxvYWRzIiwiZmlsZW5hbWUiOiJ1cGxvYWQtOTE5ODE2MGY5NGNhMjk3ZTE3NGE2MDJmNDA4OTlhM2M5Zjc2MDk5ZCIsImxlbmd0aCI6MzA3MDY4LCJrZXkiOiInZG9jcy0nLTQyMzY0MTAvdXBsb2FkLTkxOTgxNjBmOTRjYTI5N2UxNzRhNjAyZjQwODk5YTNjOWY3NjA5OWQifQ.yAfSd2cIAf2GONitgBmimMCHxvH9OYij7g0WMsiLsaE\",\n        \"filename\": \"upload-9198160f94ca297e174a602f40899a3c9f76099d\"\n    },\n    \"link\": \"https://my.freshbooks.com/service/uploads/images/eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhY2NvdW50Ijo0MjM2NDEwLCJvcmlnaW5hbF9maWxlbmFtZSI6InRvcm9udG8tc2t5bGluZS5qcGciLCJidWNrZXQiOiJ1cGxvYWRzIiwiZmlsZW5hbWUiOiJ1cGxvYWQtOTE5ODE2MGY5NGNhMjk3ZTE3NGE2MDJmNDA4OTlhM2M5Zjc2MDk5ZCIsImxlbmd0aCI6MzA3MDY4LCJrZXkiOiInZG9jcy0nLTQyMzY0MTAvdXBsb2FkLTkxOTgxNjBmOTRjYTI5N2UxNzRhNjAyZjQwODk5YTNjOWY3NjA5OWQifQ.yAfSd2cIAf2GONitgBmimMCHxvH9OYij7g0WMsiLsaE\"\n}"
    }
  ]
}