gpt4 book ai didi

java - 使用 HttpUnit 发送带有 POST 请求的文件

转载 作者:行者123 更新时间:2023-11-28 20:01:26 24 4
gpt4 key购买 nike

我们有一个 REST 服务,我们想对其进行测试。为此,我考虑过使用 HttpUnit。我们向资源 URL 发送 POST 请求,在收到请求后我们从请求中检索文件。在我们的服务器代码中,我们有这样的东西:

MultipartFormData body = request().body().asMultipartFormData();
FilePart file = body.getFile("upfile");
File pictureFile = file.getFile();

在我的测试中我写道:

WebConversation wc = new WebConversation();
WebRequest wr = new PostMethodWebRequest("http://linkToOurResource");
File f = new File("testFile.jpg");
wr.selectFile("upfile", f, "multipart/form-data;");
WebResponse response = wc.getResponse(wr);

但我收到以下错误:

Test functional.AcceptanceTests.testAddingNewClient failed: Parameter 'upfile' is not a file parameter and may not be set to a file value.

关于如何将 POST 请求中的文件发送到我们的服务器有什么建议吗?

最佳答案

您可能想阅读 httpunit 开发人员常见问题解答 - 只需搜索 httpunit 的单元测试即可找到合适的源代码示例:

https://sourceforge.net/mailarchive/forum.php?thread_name=5051BBF6.70700%40bitplan.com&forum_name=httpunit-develop

关于java - 使用 HttpUnit 发送带有 POST 请求的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10685883/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com