Sending the Object

How to contact the API#

Once we have assigned the API object to a variable describing our model, we send it to SkyCiv for analysis via the API. This is achieved by the use of a POST request. If you'd like to learn more about HTTP methods, w3schools.com provides a great definition.

There are various ways to contact the API:

  • Using the packages developed by the SkyCiv team (highly recommended).
  • Download the SkyCiv API file and include it in you project.
  • Constructing the call manually.

Packages by the SkyCiv Team#

You can view and learn about the available packages in the Packages section. We highly recommend this approach as we will continue to improve these packages to provide useful tools. This method is common industry practice as it helps keep your code clean and easy to read.

Although it may initially take longer to get setup, once you have an understanding of how package managers work (which we have provided) it will make future projects very quick and robust.

Including the SkyCiv API file#

Alternatively, you can manually include the SkyCiv API file via GitHub in your project which will make the request for you (by calling the skyciv.request() method). For example, if you're using JavaScript then you can find the module here: https://api.skyciv.com/dist/v3/javascript/skyciv.js

To use this JavaScript module, the request() function would be called as follows:

manual-use-of-skyciv-package.js
const skyciv = require('skyciv');
skyciv.request(YOUR_API_OBJECT, function (res) {
console.log(res); // do something with response
});

Manually calling the API#

You can manually make a HTTP/HTTPS POST Request to either of the following endpoints depending on whether you wish to use HTTP or HTTPS:

HTTPS: https://api.skyciv.com:8085/v3

HTTP: http://api.skyciv.com:8086/v3

The following links provide a live demonstration of how to achieve this across various languages. Just add your own credentials to the auth object!

Comments are provided to help you understand exactly what is happening. Notice that after removing the comments and print statements, the actual code is only a fraction of the file.

The code located in the above links has also been provided below.


/*
Note: We HIGHLY recommend using the skyciv NPM package to
achieve the following in a couple of lines of code!
https://www.npmjs.com/package/skyciv
*/
// Import the https module to provide access to the request method
const https = require('https');
// Import file system to let us write results to a new file
const fs = require('fs');
/*
Import the API object data - Add a file in the same directory
as this script with the name defined below.
Place your JSON object inside this file. Example:
https://repl.it/@Stevieba/SkyCiv-API-or-Nodejs-v12161-or-Simple-metric-beam
./ prefix indicates the file is in the same directory as this script
*/
const rawData = require('./input-simple-beam.json');
// Convert the JSON object into a string
const data = JSON.stringify(rawData);
// Define an object containing the http request options and assign it to the variable options for later use.
const options = {
hostname: 'api.skyciv.com', // Where to send the data
path: '/v3',
method: 'POST', // Type of http method. Further reading: http://bit.ly/http-methods
headers: {
// Describe what is being sent
'Content-Type': 'application/json',
'Content-Length': data.length,
},
};
// Create a request function that handles events during the request
const req = https
.request(options, (res) => {
// Create a variable to hold the response data
let responseData = '';
// Print the status code of the request. 200 means the message
// was successfully sent and received but does not necessarily
// mean the analysis was successful.
console.log('Status Code:', res.statusCode);
// Every time some of the response comes through, add it to the res variable
// In this case 'data' refers to an event.
res.on('data', (chunk) => {
responseData += chunk;
});
// When the entire response has been received run the code below
res.on('end', () => {
const parsedResults = JSON.parse(responseData);
if (parsedResults.response.status === 0) {
// This is a good place to start doing things with the results
// Write the results to a file named output.json for inspection
fs.writeFileSync(
'./output.json',
JSON.stringify(parsedResults, null, 2),
'utf-8'
);
console.log(
`Successfully solved, see output.json to inspect results.`
);
} else {
// This is where you can handle a failed solve
console.log(
`Unsuccessful with error message: ${parsedResults.response.msg}`
);
}
});
})
.on('error', (err) => {
// Catch any errors and print the error message
console.log('Error: ', err.message);
});
// Make the call with your data
req.write(data);
req.end();