图片上传
更新时间:2025.08.04商品券接口调用过程中需要传入各种图片URL,请使用本接口上传图片到微信支付,并使用接口返回的URL。
接口说明
支持商户:【普通服务商】
请求方式:【POST】/v3/marketing/partner/product-coupon/media/upload-image
请求域名:【主域名】https://api.mch.weixin.qq.com 使用该域名将访问就近的接入点
【备域名】https://api2.mch.weixin.qq.com 使用该域名将访问异地的接入点 ,指引点击查看
请求参数
Header HTTP头参数
Authorization 必填 string
请参考签名认证生成认证信息
Accept 必填 string
请设置为application/json
Content-Type 必填 string
请设置为multipart/form-data
Form 表单
meta 必填 object
【图片文件元信息】 使用 JSON 格式传输
属性 | |
filename 必填 string(150) 【图片文件名】 商户上传的媒体图片的名称,商户自定义,必须以JPG、BMP、PNG为后缀,名称不能超过150个字符 sha256 必填 string 【图片文件摘要】 图片文件的文件摘要,即对图片文件的二进制内容进行sha256计算得到的值 |
file 必填 file
【图片文件二进制】 媒体图片只支持JPG、BMP、PNG格式,文件大小不能超过2M
请求示例
POST
1curl -X POST \ 2 https://api.mch.weixin.qq.com/v3/marketing/partner/product-coupon/media/upload-image \ 3 -H "Authorization: WECHATPAY2-SHA256-RSA2048 mchid=\"1900000001\",..." \ 4 -H "Accept: application/json" \ 5 -H "Content-Type: multipart/form-data" \ 6 -F meta="{ 7 "filename" : "header.jpg", 8 "sha256" : "6aa6c99ce1d04afc2668154126c607af5a680734fa119e2a096529f6d6f2c0a2" 9 }" \ 10 -F file=@"/path/to/file"
需配合微信支付工具库 WXPayUtility 使用,请参考 iwiki-document:inline_state
1package com.java.demo; 2 3import com.java.utils.WXPayUtility; // 引用微信支付工具库,参考:https://pay.weixin.qq.com/doc/v3/partner/4014985777 4 5import com.google.gson.annotations.SerializedName; 6import com.google.gson.annotations.Expose; 7import okhttp3.MediaType; 8import okhttp3.OkHttpClient; 9import okhttp3.Request; 10import okhttp3.RequestBody; 11import okhttp3.Response; 12 13import java.io.IOException; 14import java.io.UncheckedIOException; 15import java.security.PrivateKey; 16import java.security.PublicKey; 17import java.util.ArrayList; 18import java.util.HashMap; 19import java.util.List; 20import java.util.Map; 21 22/** 23 * 上传图片 24 */ 25public class UploadImage { 26 private static String HOST = "https://api.mch.weixin.qq.com"; 27 private static String METHOD = "POST"; 28 private static String PATH = "/v3/marketing/partner/product-coupon/media/upload-image"; 29 30 public static void main(String[] args) { 31 // TODO: 请准备商户开发必要参数,参考:https://pay.weixin.qq.com/doc/v3/partner/4013080340 32 UploadImage client = new UploadImage( 33 "19xxxxxxxx", // 商户号,是由微信支付系统生成并分配给每个商户的唯一标识符,商户号获取方式参考 https://pay.weixin.qq.com/doc/v3/partner/4013080340 34 "1DDE55AD98Exxxxxxxxxx", // 商户API证书序列号,如何获取请参考 https://pay.weixin.qq.com/doc/v3/partner/4013058924 35 "/path/to/apiclient_key.pem" // 商户API证书私钥文件路径,本地文件路径 36 , 37 "PUB_KEY_ID_xxxxxxxxxxxxx", // 微信支付公钥ID,如何获取请参考 https://pay.weixin.qq.com/doc/v3/partner/4013038589 38 "/path/to/wxp_pub.pem" // 微信支付公钥文件路径,本地文件路径 39 ); 40 41 request = new (); 42 try { 43 UploadImageResponse response = client.run(request); 44 // TODO: 请求成功,继续业务逻辑 45 System.out.println(response); 46 } catch (WXPayUtility.ApiException e) { 47 // TODO: 请求失败,根据状态码执行不同的逻辑 48 e.printStackTrace(); 49 } 50 } 51 52 public UploadImageResponse run( request) { 53 String uri = PATH; 54 55 Request.Builder reqBuilder = new Request.Builder().url(HOST + uri); 56 reqBuilder.addHeader("Accept", "application/json"); 57 reqBuilder.addHeader("Wechatpay-Serial", wechatPayPublicKeyId); 58 reqBuilder.addHeader("Authorization", WXPayUtility.buildAuthorization(mchid, certificateSerialNo, privateKey, METHOD, uri, null)); 59 reqBuilder.addHeader("Content-Type", "application/json"); 60 RequestBody emptyBody = RequestBody.create(null, ""); 61 reqBuilder.method(METHOD, emptyBody); 62 Request httpRequest = reqBuilder.build(); 63 64 // 发送HTTP请求 65 OkHttpClient client = new OkHttpClient.Builder().build(); 66 try (Response httpResponse = client.newCall(httpRequest).execute()) { 67 String respBody = WXPayUtility.extractBody(httpResponse); 68 if (httpResponse.code() >= 200 && httpResponse.code() < 300) { 69 // 2XX 成功,验证应答签名 70 WXPayUtility.validateResponse(this.wechatPayPublicKeyId, this.wechatPayPublicKey, 71 httpResponse.headers(), respBody); 72 73 // 从HTTP应答报文构建返回数据 74 return WXPayUtility.fromJson(respBody, UploadImageResponse.class); 75 } else { 76 throw new WXPayUtility.ApiException(httpResponse.code(), respBody, httpResponse.headers()); 77 } 78 } catch (IOException e) { 79 throw new UncheckedIOException("Sending request to " + uri + " failed.", e); 80 } 81 } 82 83 private final String mchid; 84 private final String certificateSerialNo; 85 private final PrivateKey privateKey; 86 private final String wechatPayPublicKeyId; 87 private final PublicKey wechatPayPublicKey; 88 89 public UploadImage(String mchid, String certificateSerialNo, String privateKeyFilePath, String wechatPayPublicKeyId, String wechatPayPublicKeyFilePath) { 90 this.mchid = mchid; 91 this.certificateSerialNo = certificateSerialNo; 92 this.privateKey = WXPayUtility.loadPrivateKeyFromPath(privateKeyFilePath); 93 this.wechatPayPublicKeyId = wechatPayPublicKeyId; 94 this.wechatPayPublicKey = WXPayUtility.loadPublicKeyFromPath(wechatPayPublicKeyFilePath); 95 } 96 97 public static class UploadImageResponse { 98 @SerializedName("image_url") 99 public String imageUrl; 100 } 101 102} 103
需配合微信支付工具库 wxpay_utility 使用,请参考 iwiki-document:inline_state
1package main 2 3import ( 4 "bytes" 5 "demo/wxpay_utility" // 引用微信支付工具库,参考 https://pay.weixin.qq.com/doc/v3/partner/4015119446 6 "encoding/json" 7 "fmt" 8 "net/http" 9 "net/url" 10 "strings" 11) 12 13 14func main() { 15 // TODO: 请准备商户开发必要参数,参考:https://pay.weixin.qq.com/doc/v3/partner/4013080340 16 config, err := wxpay_utility.CreateMchConfig( 17 "19xxxxxxxx", // 商户号,是由微信支付系统生成并分配给每个商户的唯一标识符,商户号获取方式参考 https://pay.weixin.qq.com/doc/v3/partner/4013080340 18 "1DDE55AD98Exxxxxxxxxx", // 商户API证书序列号,如何获取请参考 https://pay.weixin.qq.com/doc/v3/partner/4013058924 19 "/path/to/apiclient_key.pem", // 商户API证书私钥文件路径,本地文件路径 20 "PUB_KEY_ID_xxxxxxxxxxxxx", // 微信支付公钥ID,如何获取请参考 https://pay.weixin.qq.com/doc/v3/partner/4013038589 21 "/path/to/wxp_pub.pem", // 微信支付公钥文件路径,本地文件路径 22 ) 23 if err != nil { 24 fmt.Println(err) 25 return 26 } 27 28 request := &{ 29 Meta: &ImageMeta{ 30 Filename: wxpay_utility.String("header.jpg"), 31 Sha256: wxpay_utility.String("6aa6c99ce1d04afc2668154126c607af5a680734fa119e2a096529f6d6f2c0a2"), 32}, 33 File: wxpay_utility.String("BINARY_DATA_HERE"), 34 } 35 36 response, err := UploadImage(config, request) 37 if err != nil { 38 fmt.Printf("请求失败: %+v\n", err) 39 // TODO: 请求失败,根据状态码执行不同的处理 40 return 41 } 42 43 // TODO: 请求成功,继续业务逻辑 44 fmt.Printf("请求成功: %+v\n", response) 45} 46 47func UploadImage(config *wxpay_utility.MchConfig, request *) (response *UploadImageResponse, err error) { 48 const ( 49 host = "https://api.mch.weixin.qq.com" 50 method = "POST" 51 path = "/v3/marketing/partner/product-coupon/media/upload-image" 52 ) 53 54 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path)) 55 if err != nil { 56 return nil, err 57 } 58 httpRequest, err := http.NewRequest(method, reqUrl.String(), nil) 59 if err != nil { 60 return nil, err 61 } 62 httpRequest.Header.Set("Accept", "application/json") 63 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId()) 64 httpRequest.Header.Set("Content-Type", "application/json") 65 authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), nil) 66 if err != nil { 67 return nil, err 68 } 69 httpRequest.Header.Set("Authorization", authorization) 70 71 client := &http.Client{} 72 httpResponse, err := client.Do(httpRequest) 73 if err != nil { 74 return nil, err 75 } 76 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse) 77 if err != nil { 78 return nil, err 79 } 80 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 { 81 // 2XX 成功,验证应答签名 82 err = wxpay_utility.ValidateResponse( 83 config.WechatPayPublicKeyId(), 84 config.WechatPayPublicKey(), 85 &httpResponse.Header, 86 respBody, 87 ) 88 if err != nil { 89 return nil, err 90 } 91 response := &UploadImageResponse{} 92 if err := json.Unmarshal(respBody, response); err != nil { 93 return nil, err 94 } 95 96 return response, nil 97 } else { 98 return nil, wxpay_utility.NewApiException( 99 httpResponse.StatusCode, 100 httpResponse.Header, 101 respBody, 102 ) 103 } 104} 105 106type UploadImageResponse struct { 107 ImageUrl *string `json:"image_url,omitempty"` 108 } 109 110 111
应答参数
200 OK
image_url 必填 string(256)
【图片URL】 微信返回的媒体文件标识url。有效期为永久
应答示例
200 OK
1{ 2 "image_url" : "https://qpic.cn/xxx" 3} 4
错误码
公共错误码
状态码 | 错误码 | 描述 | 解决方案 |
---|---|---|---|
400 | PARAM_ERROR | 参数错误 | 请根据错误提示正确传入参数 |
400 | INVALID_REQUEST | HTTP 请求不符合微信支付 APIv3 接口规则 | 请参阅 接口规则 |
401 | SIGN_ERROR | 验证不通过 | 请参阅 签名常见问题 |
500 | SYSTEM_ERROR | 系统异常,请稍后重试 | 请稍后重试 |
业务错误码
状态码 | 错误码 | 描述 | 解决方案 |
---|---|---|---|
400 | INVALID_REQUEST | 传入参数不符合业务规则 | 请参考文档中对每个字段的要求以及组合要求,确认请求参数是否满足 |
403 | NO_AUTH | 缺少业务相关权限 | 请确认已开通商品券权限 |
429 | RATELIMIT_EXCEEDED | 请求超过接口频率限制 | 请稍后使用原参数重试 |