This sample shows how you can call our Submission API using the React Native framework.
import React, { Component } from 'react';
import { StackNavigator } from 'react-navigation';
import { Text, View } from 'react-native';
class CallAPI extends Component {
constructor(props) {
super(props);
this.state = {
isLoaded: false,
isError: false,
posts: {}
};
}
componentDidMount() {
var self = this;
// Create a JSON Object first before being passed in into the Body of the Request.
var mainJSON =
{
"UserKey" : "",
"CampaignName" : "",
"ToEmail" : "",
"Subject" : "",
"SenderEmail" : "",
"SubmittedContent" : "",
"SenderName" : "",
"SubstitutionTags" : [{
"Key" : "Key0",
"Value" : "Value0"
}],
"Attachments" : [{
"Filename" : "Test.pdf",
"Content" : "SG93IHRvIGRlYnVnIFNRTCBTdG9yZWQgUHJvY2VkdXJlDQoNCjEuIExvY2FsIG9ubHkgLSBBenVyZSAoTm8pDQoyLiBQdXQgYnJlYWtwb2ludCBhdCBFWEVDIHN0YXRlbWVudA0KMy4gQ2xpY2sgRGVidWcNCjQuIE9uY2UgaGl0IGJyZWFrcG9pbnQsIHByZXNzIEYxMSB0byBzdGVwIGludG8gdGhlIHN0b3JlZCBwcm9jZWR1cmUu"
}]
}
try{
fetch("https://api.enginemailer.com/RESTAPI/Submission/SendEmail", {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type" : "application/json"
},
body: JSON.stringify(mainJSON) // Stringify the JSON Object so that it is able to be send over to the API
}).then(res => res.json()).then(function(resJson){
// Parse the JSON Object returned from the API so that we are able to retrieve the Results of the API Call
var jsonObject = JSON.parse(resJson);
self.setState({
isLoaded: true,
posts: jsonObject.Result // Assign the Result portion of the JSONObject
});
}).catch((error) =>{
console.log(error);
self.setState({
isError: true
});
})
}
catch(err){
console.error("error: " + err.message);
}
}
render() {
const { isError, isLoaded, posts } = this.state;
console.log(posts);
if(isError){
return Error;
}
if(!isLoaded){
return Loading;
}
else
{
if(posts.StatusCode == '200'){
return(
Status: Email Sent // If the call is Successful it will return StatusCode = 200
)
}
else{
return(
Status: Error
ErrorCode: {posts.StatusCode} // If the Call is unsuccessful then it will return any other Status Code.
)
}
}
}
}
export default CallAPI;