Sample code for 30+ languages & platforms
JavaScript

Amazon SP-API Upload Feed

See more Amazon SP-API Examples

Upload a feed document to the feed previously created.
Note
This example is intended for running within a Chilkat.Js embedded JavaScript engine. All Chilkat JavaScript examples require Chilkat v11.4.0 or greater.
JavaScript
var success = false;

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

// Load your own feed data file.
var sbRequestBody = new CkStringBuilder();
success = sbRequestBody.LoadFile("C:/AAWorkarea/test-despatch-upload.txt","utf-8");
if (success == false) {
    console.log("Failed to load test-despatch-upload.txt.");
    return;
}

// The SP-API Create Feed example returned JSON such as this.

// {
//   "feedDocumentId": "amzn1.tortuga.4.eu.8fb6f9c4-XXXX-4e7f-824f-XXXXXXXXXXXXX.ZZZZZZZZZZZZZZ",
//   "url": "https://tortuga-prod-eu.s3-eu-west-1.amazonaws.com/%2FNinetyDays/amzn1.tortuga.4.eu.8fb6f9c4-XXXX-4e7f-824f-XXXXXXXXXXXXX.ZZZZZZZZZZZZZZ?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20230222T014944Z&X-Amz-SignedHeaders=content-type%3Bhost&X-Amz-Expires=300&X-Amz-Credential=XXXXXXXXXXXXXXXXX%2F20230222%2Feu-west-1%2Fs3%2Faws4_request&X-Amz-Signature=XXXXXXXXXXXXXXXXXXXXXXXXXXXX"
// }

// Load the JSON feed file created by this example:  SP-API Create Feed
var jsonFeedInfo = new CkJsonObject();
success = jsonFeedInfo.LoadFile("qa_data/json/sp_api_feed_upload_info.json");
if (success == false) {
    console.log(jsonFeedInfo.LastErrorText);
    return;
}

// Get the pre-signed URL
var url = jsonFeedInfo.StringOf("url");

// We can use the Chilkat HTTP object because the URL is pre-signed.
// We don't need to add our own AWS authentication because it's already in the pre-signed URL,
// which is only valid for short amount of time.
var http = new CkHttp();

// NOTE: The string "text/tab-separated-values; charset=UTF-8" must match exactly the contentType specified when creating the feed.
var resp = new CkHttpResponse();
success = http.HttpSb("PUT",url,sbRequestBody,"utf-8","text/tab-separated-values; charset=UTF-8",resp);
if (success == false) {
    console.log(http.LastErrorText);
    return;
}

console.log("status code = " + resp.StatusCode);
console.log("status text = " + resp.StatusText);
console.log("response body:");
console.log(resp.BodyStr);