zoukankan      html  css  js  c++  java
  • REST-assured 3发送图片

    上传图片,需要media_id,从上传临时素材获取:https://work.weixin.qq.com/api/doc#10112

    https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE
    参数 :

    • access_token :调用接口凭证
    • type :媒体文件类型,分别有图片(image)、语音(voice)、视频(video),普通文件(file)

    1.封装image

    #java
    package date811;
    
    import java.util.Map;
    
    public class WXTextImage {
        /**
         *     {
         *        "touser" : "UserID1|UserID2|UserID3",
         *        "toparty" : "PartyID1|PartyID2",
         *        "totag" : "TagID1 | TagID2",
         *        "msgtype" : "image",
         *        "agentid" : 1,
         *        "image" : {
         *             "media_id" : "MEDIA_ID"
         *        },
         *        "safe":0
         *     }
         */
        private String touser;
        private String toparty;
        private String totag;
        private String msgtype;
        private Integer agentid;
        private Map<String,String> image;
        private Integer safe;
    
        public String getTouser() {
            return touser;
        }
    
        public void setTouser(String touser) {
            this.touser = touser;
        }
    
        public String getToparty() {
            return toparty;
        }
    
        public void setToparty(String toparty) {
            this.toparty = toparty;
        }
    
        public String getTotag() {
            return totag;
        }
    
        public void setTotag(String totag) {
            this.totag = totag;
        }
    
        public String getMsgtype() {
            return msgtype;
        }
    
        public void setMsgtype(String msgtype) {
            this.msgtype = msgtype;
        }
    
        public Integer getAgentid() {
            return agentid;
        }
    
        public void setAgentid(Integer agentid) {
            this.agentid = agentid;
        }
    
        public Map<String, String> getImage() {
            return image;
        }
    
        public void setImage(Map<String, String> image) {
            this.image = image;
        }
    
        public Integer getSafe() {
            return safe;
        }
    
        public void setSafe(Integer safe) {
            this.safe = safe;
        }
    }
    

    2.在公共类中获取media_id

    #java
    package date811;
    
    import io.restassured.response.Response;
    import org.testng.annotations.Test;
    
    import java.io.File;
    
    import static org.junit.Assert.assertNotNull;
    import static org.junit.Assert.assertThat;
    import static io.restassured.RestAssured.given;
    
    public class WXUtil {
        /*
        公共类
         */
        private static String corpID = "xxx";
        private static String corpSecret = "xxxxx";
        private static String tokenURL = "https://qyapi.weixin.qq.com/cgi-bin/gettoken";
        //获取access_token的方法
        public static String getToken(){
            Response res = given().param("corpid",corpID).
                    param("corpsecret",corpSecret).get(tokenURL).prettyPeek();
            String token_text = res.getBody().jsonPath().getString("access_token");
            assertNotNull(token_text);
            return token_text;
        }
        //获取media_id
        private static String uploadURL = "https://qyapi.weixin.qq.com/cgi-bin/media/upload";
        public static String getMedia(){
            String token = getToken();
            Response res = given().queryParam("access_token",token)
                    .queryParam("type","image")
                    .multiPart("media",new File("image/粘贴图片.png"))
                    .post(uploadURL).prettyPeek();
            String mediaId = res.getBody().jsonPath().getString("media_id");
            assertNotNull(mediaId);
            return mediaId;
        }
    
    }
    
    

    3.执行类

    #java
    package date811;
    import io.restassured.RestAssured.*;
    import io.restassured.http.ContentType;
    import io.restassured.matcher.RestAssuredMatchers.*;
    import io.restassured.response.Response;
    import org.hamcrest.Matchers.*;
    import org.testng.annotations.Test;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import static io.restassured.RestAssured.given;
    import static io.restassured.RestAssured.useRelaxedHTTPSValidation;
    import static org.hamcrest.Matchers.equalTo;
    //assertThat方法一定要静态导入
    import static org.hamcrest.Matchers.notNullValue;
    import static org.junit.Assert.assertThat;
    public class GetToken {
        @Test
        public void post_message() {
            String token_text = WXUtil.getToken();
            String postURL = "https://qyapi.weixin.qq.com/cgi-bin/message/send";
            WXTextImage wx = new WXTextImage();
            wx.setAgentid(0);
            wx.setSafe(0);
            wx.setToparty("0");
            wx.setMsgtype("image");
            Map<String,String> text = new HashMap<>();
            text.put("media_id",WXUtil.getMedia());
            wx.setImage(text);
            given().contentType(ContentType.JSON)
                    .body(wx)
                    .queryParam("access_token",token_text)
                    .post(postURL).prettyPrint()
        }
    }
    
    

  • 相关阅读:
    PHP htmlspecialchars和htmlspecialchars_decode(函数)
    使用CURL抓取淘宝页面
    PHP 自定义字符串中的变量名解析
    Notepad++前端开发常用插件介绍
    使用phpExcel实现Excel数据的导入导出(完全步骤)
    moment.js 日期包装类 (说明示例)
    php函数前面加&符号 和 变量前面加&符号的意义
    window 查看端口/杀进程
    eureka 去除注册中心保护机制
    mysql 表关联更新另一张表的数据
  • 原文地址:https://www.cnblogs.com/csj2018/p/9461129.html
Copyright © 2011-2022 走看看