模板渲染

模版渲染

参数说明

JSON

参数必需说明
templatestring,需要渲染模板的 id
dataobject,当 template 有值时使用,表示渲染模板的参数

data

详见每个模板的参数定义

请求样例

cURL

curl -X "POST" "https://api.kuaihaibao.com/services/screenshot" \
     -H 'Content-Type: application/json; charset=utf-8' \
     -d $'{
  "template": "3rp98k8v",
  "data": {
    "cover": "https://khb-sample.oss-cn-shanghai.aliyuncs.com/sample/bread.jpg",
    "qrcode": "https://khb-sample.oss-cn-shanghai.aliyuncs.com/sample/sample_qr_0.png",
    "title": "快海报",
    "price": "¥29.99",
    "tip": "给你推荐了一个好东西",
    "user": {
      "avatar": "https://khb-sample.oss-cn-shanghai.aliyuncs.com/sample/girl_1.jpg",
      "nickname": "晓阳"
    }
  }
}'

JavaScript (axios 版本)

axios({
  "method": "POST",
  "url": "https://api.kuaihaibao.com/services/screenshot",
  "headers": {
    "Content-Type": "application/json; charset=utf-8"
  },
  "data": {
    "template": "3rp98k8v",
    "data": {
      "cover": "https://khb-sample.oss-cn-shanghai.aliyuncs.com/sample/bread.jpg",
      "qrcode": "https://khb-sample.oss-cn-shanghai.aliyuncs.com/sample/sample_qr_0.png",
      "title": "快海报",
      "price": "¥29.99",
      "tip": "给你推荐了一个好东西",
      "user": {
        "avatar": "https://khb-sample.oss-cn-shanghai.aliyuncs.com/sample/girl_1.jpg",
        "nickname": "晓阳"
      }
    }
  }
})

Python

import requests
response = requests.post(
  url="https://api.kuaihaibao.com/services/screenshot",
  json={
    "template": "3rp98k8v",
    "data": {
      "cover": "https://khb-sample.oss-cn-shanghai.aliyuncs.com/sample/bread.jpg",
      "qrcode": "https://khb-sample.oss-cn-shanghai.aliyuncs.com/sample/sample_qr_0.png",
      "title": "快海报",
      "price": "¥29.99",
      "tip": "给你推荐了一个好东西",
      "user": {
        "avatar": "https://khb-sample.oss-cn-shanghai.aliyuncs.com/sample/girl_1.jpg",
        "nickname": "晓阳"
      }
    }
  }
)

Java

import java.io.IOException;
import org.apache.http.client.fluent.*;
import org.apache.http.entity.ContentType;

public class SendRequest
{
  public static void main(String[] args) {
    sendRequest();
  }
  
  private static void sendRequest() {
    Content content = Request.Post("https://api.kuaihaibao.com/services/screenshot")
    .addHeader("Content-Type", "application/json; charset=utf-8")
    .bodyString("{\"template\": \"3rp98k8v\",\"data\": {\"cover\": \"https://khb-sample.oss-cn-shanghai.aliyuncs.com/sample/bread.jpg\",\"qrcode\": \"https://khb-sample.oss-cn-shanghai.aliyuncs.com/sample/sample_qr_0.png\",\"title\": \"快海报\",\"price\": \"¥29.99\",\"tip\": \"给你推荐了一个好东西\",\"user\": {\"avatar\": \"https://khb-sample.oss-cn-shanghai.aliyuncs.com/sample/girl_1.jpg\",\"nickname\": \"晓阳\"}}}", ContentType.APPLICATION_JSON)
    .execute().returnContent();
    System.out.println(content);
  }
}

PHP

<?php

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.kuaihaibao.com/services/screenshot');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$json_array = [
  'template' => '3rp98k8v',
  'data' => [
    'cover' => 'https://khb-sample.oss-cn-shanghai.aliyuncs.com/sample/bread.jpg',
    'qrcode' => 'https://khb-sample.oss-cn-shanghai.aliyuncs.com/sample/sample_qr_0.png',
    'title' => '快海报',
    'price' => '¥29.99',
    'tip' => '给你推荐了一个好东西',
    'user' => [
      'avatar' => 'https://khb-sample.oss-cn-shanghai.aliyuncs.com/sample/girl_1.jpg',
      'nickname' => '晓阳'
    ]
  ]
]; 
$body = json_encode($json_array);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);

$response = curl_exec($ch);

if (!$response) {
  die('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch));
}

echo 'HTTP Status Code: ' . curl_getinfo($ch, CURLINFO_HTTP_CODE) . PHP_EOL;
echo 'Response Body: ' . $response . PHP_EOL;

curl_close($ch);

C

using System;
using System.Threading.Tasks;
using System.Net;
using System.IO;
using System.Text;

namespace MyNamespace {
	public class MyActivity {
		
		private async Task<bool> khbapisample () {

			string url = "https://api.kuaihaibao.com/services/screenshot";

			HttpWebRequest request = (HttpWebRequest)WebRequest.Create (new Uri(url));
			request.ContentType = "application/json; charset=utf-8";
			
			request.Method = "POST";
			
			string postData = "{\"template\":\"3rp98k8v\",\"data\":{\"user\":{\"avatar\":\"https://khb-sample.oss-cn-shanghai.aliyuncs.com/sample/girl_1.jpg\",\"nickname\":\"\\u6653\\u9633\"},\"tip\":\"\\u7ed9\\u4f60\\u63a8\\u8350\\u4e86\\u4e00\\u4e2a\\u597d\\u4e1c\\u897f\",\"cover\":\"https://khb-sample.oss-cn-shanghai.aliyuncs.com/sample/bread.jpg\",\"price\":\"\\u00a529.99\",\"title\":\"\\u5feb\\u6d77\\u62a5\",\"qrcode\":\"https://khb-sample.oss-cn-shanghai.aliyuncs.com/sample/sample_qr_0.png\"}}";
			ASCIIEncoding encoding = new ASCIIEncoding ();
			byte[] byte1 = encoding.GetBytes (postData);
			request.ContentLength = byte1.Length;
			Stream newStream = request.GetRequestStream ();
			newStream.Write (byte1, 0, byte1.Length);
			newStream.Close ();
			
			using (WebResponse response = await request.GetResponseAsync ()) {
				using (Stream stream = response.GetResponseStream ()) {
					return true;
					//process the response
				}
			}
		}
	}
}