React Native
React Native
Shopware Upload Media (JPG File)
See more Shopware Examples
Demonstrates how to upload a media file to a Shopware shop.Chilkat React Native Downloads
import { BinData, Http, HttpResponse, JsonObject, StringBuilder } from '@chilkat/react-native'
async function chilkatExample() {
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const http = new Http();
http.login = 'api_username';
http.password = 'api_key';
http.basicAuth = true;
// Use this online tool to generate code from sample JSON:
// Generate Code to Create JSON
// The following JSON is sent in the request body.
// {
// "file": "data:image/jpeg;base64,BASE64_DATA_HERE",
// "album": -9,
// "description": "image description",
// "userId": 5,
// "name": "my_image"
// }
const sbFileData = new StringBuilder();
sbFileData.append('data:image/jpeg;base64,');
const bdFileData = new BinData();
try {
bdFileData.loadFile('qa_data/jpg/starfish.jpg');
} catch {
console.log('Failed to load file.');
return;
}
sbFileData.append(bdFileData.getEncoded('base64'));
const json = new JsonObject();
json.updateString('file', sbFileData.getAsString());
json.updateInt('album', -9);
json.updateString('description', 'image description');
json.updateInt('userId', 5);
json.updateString('name', 'my_image');
const resp = new HttpResponse();
try {
await http.httpJsonAsync('POST', 'https://my-shopware-shop.com/api/media', json, 'application/json', resp);
} catch {
console.log(http.lastErrorText);
return;
}
const sbResponseBody = new StringBuilder();
resp.getBodySb(sbResponseBody);
const jResp = new JsonObject();
jResp.loadSb(sbResponseBody);
jResp.emitCompact = false;
console.log('Response Body:');
console.log(jResp.emit());
// A 201 response code indicates success.
const respStatusCode = resp.statusCode;
console.log(`Response Status Code = ${respStatusCode}`);
if (respStatusCode >= 400) {
console.log('Response Header:');
console.log(resp.header);
console.log('Failed.');
return;
}
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// {
// "success": true,
// "data": {
// "id": 6862,
// "location": "http:\/\/my-shopware-shop.com\/api\/media\/6862"
// }
// }
// Sample code for parsing the JSON response...
// Use the following online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
jResp.boolOf('success');
const dataId = jResp.intOf('data.id');
const dataLocation = jResp.stringOf('data.location');
}