package main

import (
	"bytes"
	"encoding/base64"
	"fmt"
	"io"
	"io/ioutil"
	"net/http"
	"os"
	"time"
)

func JsonPost() {
	d := time.Now().Format("2006-1-02")
	_, err := os.Stat(d)
	if err != nil {
		//fmt.Println("目录不存在")
		os.Mkdir(d, os.ModePerm)
		fmt.Println("创建目录")
	}
	url := "https://tysbgpu.market.alicloudapi.com/api/predict/ocr_general"
	appcode := "5bdfc02ebd4c4f31bc9d3e0b62b8ec49"
	f, err1 := os.Open("a.jpg")
	if err1 != nil {
		fmt.Println("err1")
		fmt.Println(err1)
		return
	}

	defer f.Close()
	buf := bytes.NewBuffer(nil)
	io.Copy(buf, f)
	//buf.Bytes()
	imgstr := base64.StdEncoding.EncodeToString(buf.Bytes())
	bodys := "{\"image\": \"" + imgstr + "\",\"configure\":{\"min_size\":16,\"output_prob\":true,\"output_keypoints\":false,\"skip_detection\":false,\"without_predicting_direction\":false}}"
	req, _ := http.NewRequest("POST", url, bytes.NewBuffer([]byte(bodys)))
	req.Header.Set("Content-Type", "application/json; charset=UTF-8")
	req.Header.Set("Authorization", "APPCODE "+appcode)
	client := &http.Client{}
	resp, err2 := client.Do(req)
	if err2 != nil {
		fmt.Println("err2")
		fmt.Println(err2)
		return
	}
	defer resp.Body.Close()
	// fmt.Println("status", resp.Status)
	// fmt.Println("headers", resp.Header)
	body, _ := ioutil.ReadAll(resp.Body)
	fmt.Println(string(body))
}

func main() {
	JsonPost()

}
