Back

faceplusplus - 识别身份证 - OCR - 使用 restclient 发送multipart 请求 (以及curl的问题)

发布时间: 2018-07-05 11:26:00

成功了. 看下面.

直接使用curl; 

$ curl -X POST "https://api-cn.faceplusplus.com/cardpp/v1/ocridcard" \ 
-F "api_key=Td_XijYBZUMp-COYh-Rf_kCMjqvHFHDD" \
-F "api_secret=ihehEuWYwOWM-JoQcbCPM9n2VS0cOyQl" \
-F "image_file=@scripts/id_card.jpg

参考: 官方网站  https://github.com/rest-client/rest-client

代码如下:

ENV['RAILS_ENV'] = ARGV.first || ENV['RAILS_ENV'] || 'production'
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
require 'rails'
require 'rubygems'

require 'rest-client'

url = 'https://api-cn.faceplusplus.com/cardpp/v1/ocridcard'
id_card = File.expand_path(File.dirname(__FILE__) + "/id_card.jpg")

puts "== id_card: #{id_card}"
response = RestClient.post url,
  {
    api_key: 'Td_XijYBZUMp',
    api_secret: 'ihehEuWYwOWM',
    image_file: File.open(id_card, 'rb')
  }

puts "==response: "
puts response.inspect

如何处理Exception ?  40x ,50x 都会报错. 导致我们看不到response body. 这个时候就需要打印出来:

>> begin
     RestClient.get 'http://example.com/nonexistent'
   rescue RestClient::ExceptionWithResponse => e
     e.response
   end
=> <RestClient::Response 404 "<!doctype h...">

Back