Latest update time:2019.09.09 Release notes
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.
Request URL:https://api.mch.weixin.qq.com/v3/merchant/media/upload
Request method: POST
Applicable object:Common modeInstitutional mode
API Rules: https://wechatpay-api.gitbook.io/wechatpay-api-v3
path This parameter needs to be set at the request URL
query This parameter needs to be set at the JSON for request body
body This parameter should be transferred in JSON request
Name | Variable Name | Type | Required | Description |
---|---|---|---|---|
Picture file | file | message | Yes |
body
Binary 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://api.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:6uqyGjvHzOhsLleGFQVRF-U_0 |
{
"media_id": "6uqyGjvHzOhsLleGFQVRF"
}
1.Create a POST method request/upload URI
Example: POST https://api.mch.weixin.qq.com/v3/merchant/media/upload HTTP/1.1
2. Add the file data to the request body
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.
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.
2.3 Signature calculation description:
https://wechatpay-api.gitbook.io/wechatpay-api-v3/qian-ming-zhi-nan-1/qian-ming-sheng-cheng
The request body for signature computing is the JSON string of the meta parameter:
{ "filename": "filea.jpg", "sha256": "hjkahkjsjkfsjk78687dhjahdajhk" }
Example of a string to be signed:
POST
/v3/merchant/media/upload
1566987169 //Timestamp
12ced2db6f0193dda91ba86224ea1cd8 //Random number
{"filename":" filea.jpg ","sha256":" hjkahkjsjkfsjk78687dhjahdajhk "}
3.Add HTTP header:
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"
4.Add body:
// Below is the body content
--boundary // boundaryis a custom string by the merchant.
Content-Disposition: form-data; name="meta";
Content-Type: application/json
//A blank line must be inserted here.
{ "filename": "filea.jpg", "sha256": " hjkahkjsjkfsjk78687dhjahdajhk " }
--boundary
Content-Disposition: form-data; name="file"; filename="filea.jpg";
Content-Type: image/jpg
//A blank line must be inserted here.
pic1 //pic1 is the binary content of an image.
--boundary--
Description: Each line of the request packet must end with \r\n (including blank lines).
5.Send request
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--
status code | Error Codes | 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. |
|
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证