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();
    }
}

 

你可能感兴趣的:(编程语言,记录积累)