Uploading Image API
Merchants or institutions call this API to upload the certificate images in jpeg, jpg, bmp, png format. The API should be called through POST form, with enctype set as multipart/form-data.
Merchants or institutions call this API to upload the certificate images in jpeg, jpg, bmp, png format. The API should be called through POST form, with enctype set as multipart/form-data.
Applicable to:Common mode Institutional mode
Request URL:https://apihk.mch.weixin.qq.com/v3/merchant/media/upload
Request body type:multipart/form-data
Request method:POST
Name | Variable Name | Type | Required | Description |
---|---|---|---|---|
File information | file | object | Yes | BodyBinary conversion of the media image to obtain the binary content of the media image, upload the binary content in the request body. Media images only support JPG, BMP, and PNG formats, and the file size cannot exceed 2M Example:pic1 |
Media file meta information | meta | object | Yes | BodyMedia file meta information, expressed in json, contains two objects: filename and sha256. |
String filePath = "/your/home/hellokitty.png";
URI uri = new URI("https://apihk.mch.weixin.qq.com/v3/merchant/media/upload");
File file = new File(filePath);
try (FileInputStream ins1 = new FileInputStream(file)) {
String sha256 = DigestUtils.sha256Hex(ins1);
try (InputStream ins2 = new FileInputStream(file)) {
HttpPost request = new WechatPayUploadHttpPost.Builder(uri)
.withImage(file.getName(), sha256, ins2)
.build();
CloseableHttpResponse response1 = httpClient.execute(request);
}
}
Request examples with Java, please refer to:wechatpay-apache-httpclient
Request examples with PHP, please refer to:wechatpay-guzzle-middleware
Name | Variable Name | Type | Required | Description |
---|---|---|---|---|
Media file ID | media_id | string[1, 512] | Yes | Id of the media file returned by WeChat. Example:b6a17e2a-1dc4-4376-8630-e73d06490c0d |
{
"media_id": "6uqyGjvHzOhsLleGFQVRF"
}
For example:
POST https://apihk.mch.weixin.qq.com/v3/merchant/media/upload HTTP/1.1
5.2.1 How to obtain the file parameter of an image:
The binary content of an image is included in the body of the request HTTP.
5.2.2 How to obtain the meta parameter of a media file:
Media file meta information, expressed in JSON, contains two objects: filename and sha256.
● How to obtain the filename parameter:
The custom name of an image uploaded by the merchant and must be suffixed with ".JPG", ".BMP", or ".PNG".
● How to obtain the sha256 parameter:
The image digest, obtained by calculating the binary content of an image with sha256.
5.2.3 Signature calculation::
Signature generation
The request body involved in signature calculation is a JSON string of meta:
{"filename": "filea.jpg", "sha256": "hjkahkjsjkfsjk78687dhjahdajhk"}
Example of a string to be signed:
POST
/v3/merchant/media/upload
1566987169 // Timestamp
12ced2db6f0193dda91ba86224ea1cd8 // Random number
{"filename": "file1.jpg", "sha256": "hjkahkjsjkfsjk78687dhjahdajhk"}
Content-Type:multipart/form-data.Set to the MIME media type of the object to be uploaded.
Authorization: WECHATPAY2-SHA256-RSA2048 mchid="1900231671",
nonce_str="PCHK6HSOEDTACETP6P3AL7DWPHTBKIAT",timestamp="1567067659",
serial_no="1FB89742D19F2BD30B69948D16DECA0FCB4481EB",
signature="PB6M7+3JL7TSCl5zqD1sdWVypOIEQsD4dgOU+vPiVM6GMRo2qYSWKf8u46i9ZJFhyZTBdZ7SFR+BjDZh6
89hFgN8LZL+QWTvq3cse/FEUFYyOLN7L/2IZX4GA4cWInuJ2MpOhZRMpm+emrcn42gTMKAPNQ7dBLO7ux6MoSuQp69
PW+p1ogmkER68exTVUXYqA5P3vITlWNr++RDy2+ExvB7qVISOKW0vBkxUxN9e7hwUbDwGln170ZXomoO1KpQSbw3f1u
WUCx/IlWJhJIun7rUMtVT+kfijNUqcILtSfE4hWKKVaZn9j5CX8M7aKbbDOFy3SvbSJ3WQgRnRbgog5w=="
Content-Type: multipart/form-data;boundary=boundary
// The content of the body is as follows
--boundary // boundary is a string customized by the merchant
Content-Disposition: form-data; name="meta";
Content-Type: application/json
//There must be a blank line here
{ "filename": "filea.jpg", "sha256": " hjkahkjsjkfsjk78687dhjahdajhk " }
--boundary
Content-Disposition: form-data; name="file"; filename="filea.jpg";
Content-Type: image/jpg
//There must be a blank line here
pic1 //pic1 is the binary content of an image.
--boundary--
Note:The end of each line of the request packet body needs to contain \r\n (including the blank lines).
POST /v3/merchant/media/upload HTTP/1.1
Host: api.mch.weixin.qq.com
Authorization: WECHATPAY2-SHA256-RSA2048
mchid="1900231671",nonce_str="PCHK6HSOEDTACETP6P3AL7DWPHTBKIAT",
timestamp="1567067659",
serial_no="1FB89742D19F2BD30B69948D16DECA0FCB4481EB",
signature="PB6M7+3JL7TSCl5zqD1sdWVypOIEQsD4dgOU+vPiVM6GMRo2qYSWKf8u46i9ZJFhyZTBdZ7
SFR+BjDZh689hFgN8LZL+QWTvq3cse/FEUFYyOLN7L/2IZX4GA4cWInuJ2MpOhZRMpm+emrcn42gTMKAPN
Q7dBLO7ux6MoSuQp69PW+p1ogmkER68exTVUXYqA5P3vITlWNr++RDy2+ExvB7qVISOKW0vBkxUxN9e
7hwUbDwGln170ZXomoO1KpQSbw3f1uWUCx/IlWJhJIun7rUMtVT+kfijNUqcILtSfE4hWKKVaZn9j5CX8M
7aKbbDOFy3SvbSJ3WQgRnRbgog5w=="
Content-Type: multipart/form-data;boundary=boundary
--boundary
Content-Disposition: form-data; name="meta";
Content-Type: application/json
{ "filename": "filea.jpg", "sha256": " hjkahkjsjkfsjk78687dhjahdajhk " }
--boundary
Content-Disposition: form-data; name="file"; filename="filea.jpg";
Content-Type: image/jpg
pic1
--boundary--
Error Codes | Error Message | Description | Solution |
---|---|---|---|
500 | SYSTEMERROR | System error | System error occurred when viewing a sub-vendor. Try again later. |
400 | PARAM_ERROR | Regular Validation | XXX has invalid format. Check and try again. |
PARAM_ERROR | Image format error | The image format is wrong, please check and retry. The image size should be less than 2M. Image Hash value error, please check and retry. |
|
PARAM_ERROR | FIle exceed the size limit | FIle is bigger than 2M | |
429 | FREQUENCY_LIMIT_EXCEED | Frequency limit | Operation is too fast, please retry later |
403 | NO_AUTH | No permissions to call this endpoint | No permissions. Check and try again. |
Customer Service Tel
Business Development
9:00-18:00
Monday-Friday GMT+8
Technical Support
WeChat Pay Global
ICP证