java实现发送yaml形式的数据,作为post/put请求的参数

接口发送yaml格式的数据,即:"content-type"为"application/yaml"

1.首先将yaml数据以文件形式建立,test.yaml

//此处仅举例,具体数据要符合语法格式
pipeline:
  name: xxx
  oneBuildSameTime: true
  repo:
    url: xx
    ref: xx
  trigger:
    parameters: xx 
    cron: xxx
    type: manual
    branches:
      include: xx
      exclude: xx
    triggerEvents: xx
  stages: xx
  macro: xx
  timeout: 5h

2.读取yaml文件,并改写数据,最后以yaml格式作为参数,发送post请求。

import org.apache.commons.io.FileUtils;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.testng.Assert;
import org.yaml.snakeyaml.Yaml;

import java.io.*;
import java.util.Map;

public class yaml {

    //post函数
    public static HttpResponse Post(String url,
                                    String cookie,
                                    Header[] headers,
                                    String jsonString) throws IOException {
        HttpResponse response;
        if (!url.startsWith("http://"))
            url = "http://" + url;

        CloseableHttpClient client = HttpClientBuilder.create().build();
        HttpPost post = new HttpPost(url);
        post.setEntity(new StringEntity(jsonString));
        post.setHeaders(headers);
        post.addHeader("content-type", "application/yaml");  //application/yaml
        post.addHeader("Cookie", cookie);

        response = client.execute(post);
        return response;
    }


 
    public static String postYaml() {

        String pipelineId = null;
        JSONObject jsonResult = null;
        try {
            Header[] headers = null;

---------------------------------------------
            //处理yaml文件
            Yaml yaml = new Yaml();
            Map load = (Map) yaml.load(new FileInputStream(new File("src/main/resources/test.yaml")));
            //System.out.println(yaml.dump(load).getClass().getName());
            //System.out.println(load);
            //改写某些数据
            Map load1 = (Map) load.get("pipeline");  
            load1.put("name","qqq");

            //System.out.println(yaml.dump(load));
            String tmp = yaml.dump(load);

-------------------------------------------------

            String url = ".......";
            HttpResponse response = Post(host + url,
                    cookie,
                    headers,
                    tmp);
            int responseCode = response.getStatusLine().getStatusCode();
            Assert.assertEquals(responseCode, 200);

            ....
            ...

        } catch (Exception e) {
            e.printStackTrace();
            Assert.fail("failed with Exception as:" + e);

        }

        return ...;
    }

  

    public static void main(String[] args) {
        String id = postYaml();
    }
}

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值