Upload Audio File
https://api.itniotech.com/voice/fileUpload
Parameters | Description | Required | Type |
---|---|---|---|
fileName | File name with suffix, 5-32 characters. Repetition of names is not allowed. | Yes | String |
file | File contents of base64 code (For base64 code conversion, please refer to the JAVA sample code at the very bottom of the program.) | Yes | String |
Request URL:
https://api.itniotech.com/voice/fileUpload
Request Method:
POST
Request Headers:
Content-Type: application/json;charset=UTF-8
Sign: 05d7a50893e22a5c4bb3216ae3396c7c
Timestamp: 1630468800
Api-Key: bDqJFiq9
Request Body:
{
"fileName":"test.mp3",
"file":"Base64 encoded file content"
}
Parameters | Description | Type |
---|---|---|
status | "0"means successful, others than 0 means failure, seeing Status Code description. | String |
reason | Failure reason description | String |
data | Audio file ID | String |
status | Description |
---|---|
0 | success |
-1 | Authentication error |
-2 | Restricted IP access |
-16 | Timestamp expires |
-18 | Port program unusual |
-20 | Data existing |
-21 | Data validation exception |
-22 | Parameter exception |
public static String file2Base64(File file) {
if (file == null) {
return null;
}
String base64 = null;
FileInputStream fin = null;
try {
fin = new FileInputStream(file);
byte[] buff = new byte[fin.available()];
fin.read(buff);
base64 = Base64.encode(buff);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fin != null) {
try {
fin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return base64;
}
Java
PHP
REQUEST
import cn.hutool.core.codec.Base64;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.ZoneId;
public void fileUpload() {
final String baseUrl = "https://api.itniotech.com/voice";
final String apiKey = "your api key";
final String apiPwd = "your api secret";
final String fileName = "File name";
final String file = file2Base64(new File(""));
final String url = baseUrl.concat("/fileUpload");
HttpRequest request = HttpRequest.post(url);
// generate md5 key
final String datetime = String.valueOf(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant().getEpochSecond());
final String sign = SecureUtil.md5(apiKey.concat(apiPwd).concat(datetime));
request.header(Header.CONTENT_TYPE, "application/json;charset=UTF-8")
.header("Sign", sign)
.header("Timestamp", datetime)
.header("Api-Key", apiKey);
final String body = JSONUtil.createObj()
.set("fileName", fileName)
.set("file", file)
.toString();
HttpResponse response = request.body(body).execute();
if (response.isOk()) {
String result = response.body();
System.out.println(result);
}
}
REQUEST
header('content-type:text/html;charset=utf8');
$apiKey = "your api key";
$apiSecret = "your api secret";
$url = "https://api.itniotech.com/voice/fileUpload";
$timeStamp = time();
$sign = md5($apiKey.$apiSecret.$timeStamp);
//File content encoded with base64
$base64Conent = fileToBase64('testAudio.mp3'); //The relative path of the file, demonstrated here as in the same level directory, please modify it as appropriate when using it!
$dataArr['fileName'] = 'testAudio.mp3';
$dataArr['file'] = $base64Conent;
$data = json_encode($dataArr);
$headers = array('Content-Type:application/json;charset=UTF-8',"Sign:$sign","Timestamp:$timeStamp","Api-Key:$apiKey");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 600);
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch, CURLOPT_POSTFIELDS , $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$output = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
var_dump($output);
//File to base64 encoding function
function fileToBase64($file){
$base64_file = '';
if(file_exists($file)){
$mime_type= mime_content_type($file);
$base64_data = base64_encode(file_get_contents($file));
$base64_file = 'data:'.$mime_type.';base64,'.$base64_data;
}
return $base64_file;
}
RESPONSEEXAMPLE
{
"status": "0",
"reason": "success",
"data": "1202202254d4c6372d6f341e999c7ecd0683ee464.mp3"
}