我在我们的项目中使用空手道版本 1.3.1。目前,我们有一个用例来测试 API,该 API 将文件和元数据作为请求的一部分。
我们正在使用多部分文件和字段来编写我们的功能文件,但是当我们检查报告上的 POsT 调用时,我们似乎没有看到作为请求一部分的 Json 元数据。
根据链接中的建议:https://github.com/karatelabs/karate/issues/1710
我们尝试了另一种方法来使用多部分文件和值作为参数,但它对我们不起作用。
还有其他方法可以在空手道上实现这一点吗?
注意:邮递员工作正常,没有任何问题,我们也得到了回复
如果有人遇到或解决了这个问题,请提供帮助。
以下是我的功能文件:
def temp = { 'upload': ['name':'test', 'org':123, 'branch': 'xyz', 'amount': 100], 'info': 8900, 'id': 123}
Given url "http://11.111.1.111:1111"
And path "/api/v1/upload
header Content-Type = 'multipart/form-data'
And multipart file file = {read: 'classpath:resources/file/file1.txt', filename: 'file1.txt'}
And multipart file metadata = {value: '#(temp)}
And method POST
print response
注意:我也尝试过*多部分字段元数据= temp(但这里没有运气)
回应:
content-disposition: form-data; name= file; filename="file1.txt"
content-type: text/plain; charset= UTF-8
content-length: 5300
Completed: true
IsInMemory: true
content-disposition: form-data; name= metadata; filename=""
content-type: application/json; charset= UTF-8
content-length: 100
Completed: true
IsInMemory: true
问题是当我在空手道上运行功能文件时,API 返回 200 并带有空白响应。 回复:{}
但是当从 Postman 执行相同操作时,它会返回我的响应 邮递员的回复:{id:123,状态:‘完成’}
我还尝试使用 Java okhttpClient 请求执行相同的请求,它工作正常并给了我预期的响应。
OkHttpClient 的 Java 类的实现(正在运行):
import okhttp3.*;
import java.io.File;
import java.io.IOException;
import java.util.Map;
public class OkHttpHelper {
private OkHttpClient client;
public OkHttpHelper() {
client = new OkHttpClient();
}
public String executeMultipartRequest(String url, Map<String, String> fields, Map<String, String> files) throws IOException {
MultipartBody.Builder requestBodyBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM);
// Add fields
if (fields != null) {
for (Map.Entry<String, String> field : fields.entrySet()) {
requestBodyBuilder.addFormDataPart(field.getKey(), null, RequestBody.create(MediaType.parse("application/json"), field.getValue()));
}
}
// Add files
if (files != null) {
for (Map.Entry<String, String> file : files.entrySet()) {
File uploadFile = new File(file.getValue());
requestBodyBuilder.addFormDataPart(file.getKey(), uploadFile.getName(),
RequestBody.create(MediaType.parse("text/plain"), uploadFile));
}
}
RequestBody requestBody = requestBodyBuilder.build();
// Print requestBody content
System.out.println("Request Body: " + requestBodyToString(requestBody));
Request request = new Request.Builder()
.url(url)
.post(requestBody)
.build();
try (Response response = client.newCall(request).execute()) {
return response.body().string();
}
}
private String requestBodyToString(RequestBody requestBody) throws IOException {
Buffer buffer = new Buffer();
requestBody.writeTo(buffer);
return buffer.readUtf8();
}
}
功能文件如下所示:
Feature: Multipart Request with OkHttp
Background:
* def OkHttpHelper = Java.type('com.example.OkHttpHelper')
* def okHttpHelper = new OkHttpHelper()
Scenario: Send Multipart Request
* def url = 'https://example.com/upload'
And def fields = { "metadata": '{"key1": "value1"}' }
And def files = { "file": "path/to/file1.txt" }
When def response = okHttpHelper.executeMultipartRequest(url, fields, files)
Then print response
** 来自 POSTMAN 的 CURL 命令**
curl -X POST \
http://11.111.11.111:1111/api/v1/fileupload
-H 'Accept: */*' \
-H 'Accept-Encoding: gzip, deflate' \
-H 'Cache-Control: no-cache' \
-H 'Connection: keep-alive' \
-H 'Content-Length: 5800' \
-H 'Content-Type: multipart/form-data; boundary=---------23434234234343' \
-H 'Host: 11.111.11.111:1111' \
-H 'Postman-Token: XXXXXX-xxxx-xxxx-xxxx-Xxxxxxx,XXXXXXXX' \
-H 'User-Agent: PostmanRuntime/X.XX.X' \
-H 'cache-control: no-cache' \
-H 'content-type: multipart/form-data; boundary=---------23434234234343' \
-F file@/C:/Test/Sample/file1.txt \
-F ‘metadata’= { "metadata": '{"key1": "value1"}' }
据我所知,这应该有效:
* url 'https://httpbin.org/anything'
* multipart file file = { read: 'myfile.txt' }
* multipart field metadata = { some: 'data' }
* method post
您可以通过将 okhttp 或 postman 测试指向
https://httpbin.org/anything
进行实验,看看有什么区别。响应将告诉您请求中发送的所有字段值和文件值。