zoukankan      html  css  js  c++  java
  • REST-assured 2发送文字到接口

    获取token


    https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ID&corpsecret=SECRECT

    #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 static io.restassured.RestAssured.given;
    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 getToken(){
            /**
             * 获取access token
             */
            String tokenUrl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken";
            String corpId = "xxxxxx";
            String corpSecret = "xxxxxxx";
            Response res = given().param("corpid",corpId).param("corpsecret",corpSecret).get(tokenUrl).prettyPeek();
            String token_id = res.getBody().jsonPath().getString("access_token");
            res.then().statusCode(equalTo(200));
            assertThat(token_id,notNullValue());
        }
    }
    
    

    发送文字到接口

    #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 static io.restassured.RestAssured.given;
    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 postMessage(){
            /**
             * 获取access token
             */
            String tokenUrl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken";
            String corpId = "xxxxxx";
            String corpSecret = "xxxxxxxxxx";
            Response res = given().param("corpid",corpId).param("corpsecret",corpSecret).get(tokenUrl).prettyPeek();
            String token_id = res.getBody().jsonPath().getString("access_token");
            res.then().statusCode(equalTo(200));
            assertThat(token_id,notNullValue());
    
            /**
             * 发送消息
             *     {
             *        "touser" : "UserID1|UserID2|UserID3",
             *        "toparty" : "PartyID1|PartyID2",
             *        "totag" : "TagID1 | TagID2",
             *        "msgtype" : "text",
             *        "agentid" : 1,
             *        "text" : {
             *            "content" : "你的快递已到,请携带工卡前往邮件中心领取。
    出发前可查看<a href="http://work.weixin.qq.com">邮件中心视频实况</a>,聪明避开排队。"
             *        },
             *        "safe":0
             *     }
             */
            
            String req ="    {
    " +
                    "       "toparty" : "1",
    " +
                    "       "msgtype" : "text",
    " +
                    "       "agentid" : 1,
    " +
                    "       "text" : {
    " +
                    "           "content" : "你的快递已到,请携带工卡前往邮件中心领取。\n出发前可查看<a href=\"http://work.weixin.qq.com\">邮件中心视频实况</a>,聪明避开排队。"
    " +
                    "       },
    " +
                    "       "safe":0
    " +
                    "    }";
            /**
             * 传参有2中方法:1,将参数加入URL 2.使用queryParam传入参数
             */
            String post_url1 = "https://qyapi.weixin.qq.com/cgi-bin/message/send";
            String post_url2 = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token="+token_id;
            
            System.out.println("方法1");
            given().contentType(ContentType.JSON).queryParam("access_token",token_id).body(req).post(post_url1).prettyPeek();
            System.out.println("方法2");
            given().contentType(ContentType.JSON).body(req).post(post_url2).prettyPeek();
        }
    }
    
    

  • 相关阅读:
    使用gzip优化web应用(filter实现)
    在Spring、Hibernate中使用Ehcache缓存(2)
    Ehcache 整合Spring 使用页面、对象缓存(1)
    ehcache-----在spring和hibernate下管理ehcache和query cache
    使用Spring MVC构建REST风格WEB应用
    开涛spring3(12.4)
    开涛spring3(12.3)
    LLE局部线性嵌入算法
    图像固定条纹干扰
    多尺度字典学习超分辨率——相关中文文献
  • 原文地址:https://www.cnblogs.com/csj2018/p/9460653.html
Copyright © 2011-2022 走看看