这可能是一个愚蠢的问题,但我一直在努力寻找答案。我需要通过发布请求将测试屏幕快照上传到图像比较工具,该客户端的客户端代码是在Ruby上编写的,我需要创建自己的Java客户端,当前卡在400(错误请求)上,不幸的是,我没有来自服务器的日志它没有暴露,但是红宝石客户端工作正常。 您能否告诉我请求主体的外观如何? (这是我第一次接触多部分)
Ruby代码
def submit_test(options = {})
source_url = options[:source_url] || ''
fuzz_level = options[:fuzz_level] || ''
highlight_colour = options[:highlight_colour] || ''
request = RestClient::Request.execute(
method: :post,
url: "#{@url_base}/tests",
timeout: 120,
multipart: true,
payload: {
test: {
run_id: @run_id,
name: options[:name],
browser: options[:browser],
size: options[:size],
screenshot: options[:screenshot],
source_url: source_url,
fuzz_level: fuzz_level,
highlight_colour: highlight_colour,
crop_area: options[:crop_area]
}
}
)
我的Java代码
@Test
public void spectre() throws MalformedURLException, JSONException, IOException {
BufferedImage image = ImageIO.read(new File("C:\\Users\\user\\Desktop\\testfile.JPG"));
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ImageIO.write(image, "png", outputStream);
final byte[] bytes = outputStream.toByteArray();
JSONObject test = new JSONObject();
given()
.multipart("run_id", 1)
.multipart("name", "Landing Page")
.multipart("browser", "firefox")
.multipart("size", 1024)
.multiPart("screenshot", "file1.png", bytes)
.post("http://10.0.33.179:3333/tests");
System.out.println(then().extract().statusCode());
}