Login expired. Please log in again.

Feedback

0/300

Feedback

Submitted successfully

ok

Feedback

Network exception, please try again later

ok

Uploading Image(For HK)

Public API
Uploading Image(For HK)
Help Center
Contact Us

Uploading Image API(For HK)

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.


API Intro

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


Request Parameters

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.
Name Variable Name Type Required Description

file name

filename string[1,128]

No

The name of the media image uploaded by the merchant is customized by the merchant and must be suffixed with JPG, BMP, and PNG.
Example:filea.jpg

Document summary

sha256 string[1,64]

No

The file summary of the picture file is the value obtained by sha256 calculation of the binary content of the picture file
Example:hjkahkjsjkfsjk78687dhjahdajhk

Request examples with different development languages


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);
  }
}
    

// 参考上述指引说明,并引入 `MediaUtil` 正常初始化,无额外条件
use WechatPay\GuzzleMiddleware\Util\MediaUtil;
// 实例化一个媒体文件流,注意文件后缀名需符合接口要求
$media = new MediaUtil('/your/file/path/with.extension');

// 正常使用Guzzle发起API请求
try {
    $resp = $client->request('POST', 'https://api.mch.weixin.qq.com/v3/[merchant/media/video_upload|marketing/favor/media/image-upload]', [
        'body'    => $media->getStream(),
        'headers' => [
            'Accept'       => 'application/json',
            'content-type' => $media->getContentType(),
        ]
    ]);
    // POST 语法糖
    $resp = $client->post('merchant/media/upload', [
        'body'    => $media->getStream(),
        'headers' => [
            'Accept'       => 'application/json',
            'content-type' => $media->getContentType(),
        ]
    ]);
    echo $resp->getStatusCode().' '.$resp->getReasonPhrase()."\n";
    echo $resp->getBody()."\n";
} catch (Exception $e) {
    echo $e->getMessage()."\n";
    if ($e->hasResponse()) {
        echo $e->getResponse()->getStatusCode().' '.$e->getResponse()->getReasonPhrase()."\n";
        echo $e->getResponse()->getBody();
    }
    return;
}
    

Request examples with Java, please refer to:wechatpay-apache-httpclient

Request examples with PHP, please refer to:wechatpay-guzzle-middleware

Returned Result

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

Return example:


{
"media_id": "6uqyGjvHzOhsLleGFQVRF"
}
                                

    http://2323weixin.qq.com
                                

Request steps

Image upload steps:

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--

Error Code

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.
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.

About  WeChat  Pay

Powered By Tencent & Tenpay Copyright©

2005-2019 Tenpay All Rights Reserved.

Contact Us
Wechat Pay Global

Wechat Pay Global

ICP证

粤B1.B2-20090295