Chilkat Online Tools

Swift / PrestaShop Webservice Product Resource A-Z / 6 - Upload image

Back to Collection Items

func chilkatTest() {
    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    let http = CkoHttp()
    var success: Bool

    http.BasicAuth = true
    http.Login = "{{webservice_key}}"
    http.Password = "password"

    let req = CkoHttpRequest()
    req.HttpVerb = "POST"
    req.Path = "/api/images/products/1"
    req.ContentType = "multipart/form-data"
    success = req.AddFileForUpload2("image", path: " path to file", contentType: "application/octet-stream")

    req.AddHeader("Expect", value: "100-continue")

    var resp: CkoHttpResponse? = http.SynchronousRequest("localhost", port: 8080, ssl: true, req: req)
    if http.LastMethodSuccess == false {
        print("\(http.LastErrorText)")
        return
    }

    print("\(resp!.StatusCode.intValue)")
    print("\(resp!.BodyStr)")
    resp = nil

}

Curl Command

curl -X POST
	-u '{{webservice_key}}:password'
	-H "Content-Type: application/json"
	--form 'image=@"/path/to/file"'
https://localhost:8080/api/images/products/1

Postman Collection Item JSON

{
  "name": "6 - Upload image",
  "event": [
    {
      "listen": "prerequest",
      "script": {
        "exec": [
          "",
          ""
        ],
        "type": "text/javascript"
      }
    }
  ],
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "value": "application/json",
        "type": "text"
      }
    ],
    "body": {
      "mode": "formdata",
      "formdata": [
        {
          "key": "image",
          "type": "file",
          "src": [
          ]
        }
      ]
    },
    "url": {
      "raw": "{{webservice_url}}/api/images/products/{{product_id}}",
      "host": [
        "{{webservice_url}}"
      ],
      "path": [
        "api",
        "images",
        "products",
        "{{product_id}}"
      ]
    }
  },
  "response": [
  ]
}