API调用样例
示例接口
以身份证识别接口为例(其他接口URL参考对应接口文档)
1.HTTP 样例
POST /ocr/idcard HTTP/1.1
Host: cloudapi.deepfinch.com
X-DF-API-SECRET: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
X-DF-API-ID: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Cache-Control: no-cache
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="file"; filename="idcard3.jpg"
Content-Type: image/jpeg
...
------WebKitFormBoundary7MA4YWxkTrZu0gW--
2.cURL 样例
curl -X POST \
https://cloudapi.deepfinch.com/ocr/idcard \
-H 'cache-control: no-cache' \
-H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
-H 'X-DF-API-ID: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' \
-H 'X-DF-API-SECRET: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' \
-F file=@idcard3.jpg
3.C 样例
CURL *hnd = curl_easy_init();
curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(hnd, CURLOPT_URL, "https://cloudapi.deepfinch.com/ocr/idcard");
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "cache-control: no-cache");
headers = curl_slist_append(headers, "X-DF-API-ID: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
headers = curl_slist_append(headers, "X-DF-API-SECRET: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
headers = curl_slist_append(headers, "content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"file\"; filename=\"idcard3.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--");
CURLcode ret = curl_easy_perform(hnd);
4.java 样例
import java.io.IOException;
import java.io.File;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.MultipartBody;
public class PostExample {
final MediaType MEDIA_TYPE_JPG = MediaType.parse("image/jpg");
String post(String file1, String file2) throws IOException {
OkHttpClient client = new OkHttpClient();
RequestBody body = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("file", "1.jpg", RequestBody.create(MEDIA_TYPE_JPG, new File(file1)))
.build();
Request request = new Request.Builder()
.addHeader("X-DF-API-ID", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
.addHeader("X-DF-API-SECRET", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
.url("https://cloudapi.deepfinch.com/ocr/idcard")
.post(body)
.build();
Response response = client.newCall(request).execute();
return response.body().string();
}
public static void main(String[] args) throws IOException {
PostExample example = new PostExample();
String response = example.post("./1.jpg", "./2.jpg");
System.out.println(response);
}
}
5.nodejs 样例
var http = require("https");
var options = {
"method": "POST",
"hostname": "cloudapi.deepfinch.com",
"port": null,
"path": "/ocr/idcard",
"headers": {
"content-type": "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
"X-DF-API-SECRET": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"X-DF-API-ID": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}
};
var req = http.request(options, function (res) {
var chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function () {
var body = Buffer.concat(chunks);
console.log(body.toString());
});
});
req.write("------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"file\"; filename=\"idcard3.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--");
req.end();
6.php 样例
<?php
require_once 'HTTP/Request2.php';
$request = new HTTP_Request2();
$request->setUrl('https://cloudapi.deepfinch.com/ocr/idcard');
$request->setMethod(HTTP_Request2::METHOD_POST);
$request->setConfig(array(
'follow_redirects' => TRUE
));
$request->setHeader(array(
'X-DF-API-ID' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'X-DF-API-SECRET' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'Accept' => '*/*',
'Cache-Control' => 'no-cache',
'Host' => 'cloudapi.deepfinch.com',
'Accept-Encoding' => 'gzip, deflate, br',
'Content-Length' => '196880',
'Connection' => 'keep-alive'
));
$request->addUpload('image', '/file/to/path', 'filename', '<Content-Type Header>');
try {
$response = $request->send();
if ($response->getStatus() == 200) {
echo $response->getBody();
}
else {
echo 'Unexpected HTTP status: ' . $response->getStatus() . ' ' .
$response->getReasonPhrase();
}
}
catch(HTTP_Request2_Exception $e) {
echo 'Error: ' . $e->getMessage();
}
7.ruby 样例
require 'uri'
require 'rest-client'
url = URI("https://cloudapi.deepfinch.com/ocr/idcard")
headers = {
X-DF-API-SECRET: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
X-DF-API-ID: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
}
params = {
file: File.new(filepath, 'rb'),
multipart: true
}
response = RestClient::Request.execute method: :post, url: url, payload: params, headers: headers, timeout: timeout
puts response.body
8.python 样例
import requests
url = "https://cloudapi.deepfinch.com/ocr/idcard"
headers = {
'X-DF-API-SECRET': "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
'X-DF-API-ID': "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
}
files = {'file': open(filepath, 'rb')}
response = requests.post(api_host, headers=headers, files=files)
print(response.text)