我正在尝试制作一个运行Java的Postman API的应用程序,我正准备获得所有信息,并试图发出请求并获得响应。我从Postman获得了API代码,如图所示下方:
package com.example.tool.postman;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import java.io.IOException;
public class PostmanExample {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("{{url}}/example1/{{example2}}/endpoint/endpoint")
.get()
.addHeader("Content-Type", "application/json")
.addHeader("Authorization",
"aValue Credential={{credential}}/example/example/example/_request,SignedHeaders=content-type;host;x-amz-content-sha256;x-amz-date,Signature={{signature}}")
.addHeader("example", "{{exampleValue}}")
.addHeader("appAgent", "Postman")
.addHeader("cache-control", "no-cache")
.addHeader("Postman-Token", "aPostmanToken")
.build();
Response response = client.newCall(request).execute();
public PostmanExample() throws IOException {
}
public Request getRequest() {
return request;
}
public Response getResponse() {
return response;
}
}
我在不同的班级拨打电话,如下所示:
package com.example.tool.controller;
import com.example.tool.PostmanExample
import java.io.IOException;
public class ToolExample extends PostmanExample {
public ToolExample() throws IOException {
}
public static void main(String[] args) throws IOException {
PostmanExample postmanExample = new PostmanExample();
System.out.println(postmanExample.getResponse().toString());
}
}
问题是在发出请求之前,邮递员中有一个预请求脚本,该脚本执行以下操作:
console.log('Pre-request Script');
eval(environment['prerequest_signing_script']);
var key = environment['keyvalue'];
var keyname = environment['keyname'];
functions.signCall(request, key, keyname);
我该如何使用Java执行此预请求脚本或通过传递这两个参数来对我的呼叫签名?
答案 0 :(得分:0)
在Java中无法做到这一点,最好的选择是使用Newman。构造您的集合,然后通过纽曼执行它。如果出于特定原因需要Java,则可以将Newman可执行文件包装在Java程序中。
这是一篇很好的入门文章:
https://learning.getpostman.com/docs/postman/collection_runs/command_line_integration_with_newman/