Login expired. Please log in again.

Feedback

0/300

Feedback

Submitted successfully

ok

Feedback

Network exception, please try again later

ok

Uploading Image API

Latest update time:2022.08.04 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.

1. API Instructions

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


Pathparameter is a path parameter.
Queryparameter needs to be passed in the request URL.
Bodyparameter needs to be passed in the request JSON.

2. Request Parameters

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.
Name Variable Name Type Required Description
File name filename string[1, 128] Yes 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:verify.jpg
Document summary sha256 string[1, 64] Yes The file summary of the picture file is the value obtained by sha256 calculation of the binary content of the picture file
Example:ff3f4036a1164d1ddbad5b3edf9022addb3e1961a54a922708a6c1ffc49e5489

3. Request examples with different development languages

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

// 正常使用Guzzle发起API请求
try {
    $resp = $client->request('POST', 'https://apihk.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;
}

									{
										"stock_id": ".NET",
										"limit": 10,
									}

									{
										"stock_id": "Python",
										"stock_creator_mchid": "123456",
										"limit": 10,
									}

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

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

4. Return Parameters

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

Return Example

{
  "media_id": "6uqyGjvHzOhsLleGFQVRF"
}

{
    "appid": "wx7bc98d929da735fe",

  }

									{
										"stock_id": ".NET",
										"limit": 10,
									}

									{
										"stock_id": "Python",
										"stock_creator_mchid": "123456",
										"limit": 10,
									}

5. Request Procedure

5.1. Create a POST method request/upload URI

For example:

POST https://apihk.mch.weixin.qq.com/v3/merchant/media/upload HTTP/1.1

{
    "appid": "wx7bc98d929da735fe",

  }

									{
										"stock_id": ".NET",
										"limit": 10,
									}

									{
										"stock_id": "Python",
										"stock_creator_mchid": "123456",
										"limit": 10,
									}

5.2.Add the file data to the request body

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

5.3.Add the 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

5.4.Add a body:

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

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

6. Error Code

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.




    Page Navigation

About  WeChat  Pay

Powered By Tencent & Tenpay Copyright©

2005-2024 Tenpay All Rights Reserved.

Contact Us
Wechat Pay Global

WeChat Pay Global

置顶