Dart
Dart
Amazon SP-API Create Feed Doc
See more Amazon SP-API Examples
Creates a feed document. Amazon returns a feedDocumentId value, encryption details, and a URL for uploading the feed contents.Chilkat Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
final authAws = CkAuthAws();
authAws.accessKey = 'AWS_ACCESS_KEY';
authAws.secretKey = 'AWS_SECRET_KEY';
authAws.serviceName = 'execute-api';
// Use the region that is correct for your needs.
authAws.region = 'eu-west-1';
final rest = CkRest();
try {
rest.connect('sellingpartnerapi-eu.amazon.com', 443, true, true);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
rest.setAuthAws(authAws);
// Load the previously obtained LWA access token.
// See Fetch SP-API LWA Access Token
final jsonToken = CkJsonObject();
try {
jsonToken.loadFile('qa_data/tokens/sp_api_lwa_token.json');
} on ChilkatException {
print('Failed to load LWA access token.');
return;
}
// Add the x-amz-access-token request header.
final lwaToken = jsonToken.stringOf('access_token');
final jsonReq = CkJsonObject();
jsonReq.updateString('contentType', 'text/tab-separated-values; charset=UTF-8');
final sbRequest = CkStringBuilder();
jsonReq.emitSb(sbRequest);
rest.clearAllQueryParams();
rest.clearAllHeaders();
rest.addHeader('x-amz-access-token', lwaToken);
final sbResponse = CkStringBuilder();
final path = '/feeds/2021-06-30/documents';
try {
rest.fullRequestSb('POST', path, sbRequest, sbResponse);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// Examine the response status.
final statusCode = rest.responseStatusCode;
print('statusCode: $statusCode');
if (statusCode != 201) {
print('Response status text: ${rest.responseStatusText}');
print('Response body: ');
print(sbResponse.getAsString());
print('Failed.');
return;
}
print(sbResponse.getAsString());
// If successful, gets a JSON response such as the following:
// {
// "feedDocumentId": "3d4e42b5-1d6e-44e8-a89c-2abfca0625bb",
// "url": "https://d34o8swod1owfl.cloudfront.net/Feed_101__POST_PRODUCT_DATA_.xml"
// }
// Use this online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
final json = CkJsonObject();
json.loadSb(sbResponse);
final feedDocumentId = json.stringOf('feedDocumentId');
final url = json.stringOf('url');
// Save the JSON to a file for the example that uploads the feed..
json.writeFile('qa_data/json/sp_api_feed_upload_info.json');
print('Success!');
}