POST 加特林请求
POST request in Gatling
我 运行 遇到了 pdf 文件 POST 的问题。在 Gatling 2.1.7 中录制 HAR 文件后,这是我所拥有的:
.exec(http("request_10")
.post("/api/data/files?revisionId=e9af2c93-d8df-4424-b307-df4c4abbaad1&uploadType=read_only_file&fileType=application%2Fpdf&fileName=testdocument.pdf&fileSize=10080©ToEditable=true")
.headers(Map(
"Accept-Encoding" -> "gzip, deflate",
"Content-Type" -> "multipart/form-data; boundary=----WebKitFormBoundaryawCJ4mjL1imiO7Ye"
"Origin" -> url))
.body(RawFileBody("RecordedSimulation_0010_request.txt")))
与RecordedSimulation_0010_request.txt
的内容:
------WebKitFormBoundaryawCJ4mjL1imiO7Ye
Content-Disposition: form-data; name="resumableChunkNumber"
1
------WebKitFormBoundaryawCJ4mjL1imiO7Ye
Content-Disposition: form-data; name="resumableChunkSize"
1048576
------WebKitFormBoundaryawCJ4mjL1imiO7Ye
Content-Disposition: form-data; name="resumableCurrentChunkSize"
10080
------WebKitFormBoundaryawCJ4mjL1imiO7Ye
Content-Disposition: form-data; name="resumableTotalSize"
10080
------WebKitFormBoundaryawCJ4mjL1imiO7Ye
Content-Disposition: form-data; name="resumableType"
application/pdf
------WebKitFormBoundaryawCJ4mjL1imiO7Ye
Content-Disposition: form-data; name="resumableIdentifier"
66dc65bf-265d-4363-96fd-7fc13f8ceda4
------WebKitFormBoundaryawCJ4mjL1imiO7Ye
Content-Disposition: form-data; name="resumableFilename"
testdocument.pdf
------WebKitFormBoundaryawCJ4mjL1imiO7Ye
Content-Disposition: form-data; name="resumableRelativePath"
testdocument.pdf
------WebKitFormBoundaryawCJ4mjL1imiO7Ye
Content-Disposition: form-data; name="resumableTotalChunks"
1
------WebKitFormBoundaryawCJ4mjL1imiO7Ye
Content-Disposition: form-data; name="file"; filename="blob"
Content-Type: application/octet-stream
------WebKitFormBoundaryawCJ4mjL1imiO7Ye--
当我尝试播放它时这不起作用(可能是由于唯一 ID),所以(在 bodies 文件夹中粘贴 testdocument.pdf
之后)我做了以下操作:
val documentFeeder = Iterator.continually(Map(
"documentBoundary" -> (Random.alphanumeric.take(16).mkString),
"documentUuid" -> ((Random.alphanumeric.take(8).mkString + "-" +
Random.alphanumeric.take(4).mkString + "-" +
Random.alphanumeric.take(4).mkString + "-" +
Random.alphanumeric.take(4).mkString + "-" +
Random.alphanumeric.take(12).mkString).toLowerCase)))
...
.feed(documentFeeder)
// a previous exec POST with a check to grab the documentRevisionId
.exec(http("Post document: upload the file")
.post("/api/data/files")
.queryParamMap(Map(
"revisionId" -> "${documentRevisionId}",
"uploadType" -> "read_only_file",
"fileType" -> "application%2Fpdf",
"fileName" -> "testdocument.pdf",
"fileSize" -> "10080",
"copyToEditable" -> "true"))
.headers(Map(
"Accept-Encoding" -> "gzip, deflate",
"Content-Type" -> "multipart/form-data; boundary=----WebKitFormBoundary${documentBoundary}"
"Origin" -> url))
.body(StringBody("""------WebKitFormBoundary${documentBoundary}
Content-Disposition: form-data; name="resumableChunkNumber"
1
------WebKitFormBoundary${documentBoundary}
Content-Disposition: form-data; name="resumableChunkSize"
1048576
------WebKitFormBoundary${documentBoundary}
Content-Disposition: form-data; name="resumableCurrentChunkSize"
10080
------WebKitFormBoundary${documentBoundary}
Content-Disposition: form-data; name="resumableTotalSize"
10080
------WebKitFormBoundary${documentBoundary}
Content-Disposition: form-data; name="resumableType"
application/pdf
------WebKitFormBoundary${documentBoundary}
Content-Disposition: form-data; name="resumableIdentifier"
${documentUuid}
------WebKitFormBoundary${documentBoundary}
Content-Disposition: form-data; name="resumableFilename"
testdocument.pdf
------WebKitFormBoundary${documentBoundary}
Content-Disposition: form-data; name="resumableRelativePath"
testdocument.pdf
------WebKitFormBoundary${documentBoundary}
Content-Disposition: form-data; name="resumableTotalChunks"
1
------WebKitFormBoundary${documentBoundary}
Content-Disposition: form-data; name="file"; filename="blob"
Content-Type: application/octet-stream
Content-Transfer-Encoding: BINARY
------WebKitFormBoundary${documentBoundary}--""")))
最后,这是我们应用程序中 POST 虚拟数据的 Node.js 代码(我使用它作为参考,因为我知道它有效):
var resumableData = {
resumableChunkNumber: 1,
resumableChunkSize: 1048576,
resumableCurrentChunkSize: file.size,
resumableTotalSize: file.size,
resumableType: guessBestMimeType(file.name, file.type),
resumableIdentifier: genUuid(),
resumableFilename: file.name,
resumableRelativePath: file.name,
resumableTotalChunks:1
};
var boundaryKey = Math.random().toString(16); // random string
// the header for the one and only part (need to use CRLF here)
var resumableBody = '';
for(var resumablePart in resumableData){
if(resumableData.hasOwnProperty(resumablePart)){
resumableBody +=
'--' + boundaryKey + '\r\n' +
'Content-Disposition: form-data; name="' + resumablePart + '"\r\n\r\n' +
resumableData[resumablePart] + '\r\n';
}
}
resumableBody +=
'--' + boundaryKey + '\r\n' +
'Content-Disposition: form-data; name="file"; filename="blob"\r\n' +
// use your file's mime type here, if known
'Content-Type: application/octet-stream\r\n' +
'Content-Transfer-Encoding: BINARY\r\n\r\n';
var resumableEnd = '\r\n--' + boundaryKey + '--';
var request = https.request({
method : 'POST',
host : config.API_HOST + config.BASE_URL,
port : config.API_PORT,
path : generateUrl(documentRevision, file, fileType, convertEditable, copyToEditable),
headers : {
'Content-Type': 'multipart/form-data; boundary='+boundaryKey,
'Content-Length' : file.size + Buffer.byteLength(resumableBody + resumableEnd, 'utf-8')
}
}, function (response) {
var data = '';
response.on('data', function(chunk) {
data += chunk.toString();
});
response.on('end', function() {
resolve(JSON.parse(data));
});
response.on('error', function(err){
console.error(err);
reject(err);
});
});
request.write(resumableBody);
fs.createReadStream(file.path, { bufferSize: 4 * 1024})
.on('end', function() {
request.end(resumableEnd);
})
.pipe(request, { end: false });
几天来我一直在努力解决这个问题,这是我第一次涉足 Scala 和 Gatling。我在这里缺少什么才能得到这个 POST?
一个突出的问题(虽然它可能是一个转移注意力的问题)是我的应用程序在 header 中使用了 Content-Length
- 这是否有必要,因为 Gatling 已经省略了它?如果有必要,我是否可以在不硬编码的情况下将数字插入 Gatling 中?
编辑
阅读这篇文章后 post 我尝试了以下方法:
.exec(http("test post")
.post("/api/data/files")
.headers(Headers.headers_6)
.formParamMap(Map(
"revisionId" -> "${documentRevisionId}",
"uploadType" -> "read_only_file",
"fileType" -> "application%2Fpdf",
"fileName" -> "testdocument.pdf",
"fileSize" -> "10080",
"copyToEditable" -> "true"))
.bodyPart(StringBodyPart("""{ "resumableChunkNumber": "1",
"resumableChunkSize": "1048576",
"resumableCurrentChunkSize": "10080",
"resumableTotalSize": "10080",
"resumableType": "application/pdf",
"resumableIdentifier": "${documentUuid}",
"resumableFilename": "testdocument.pdf",
"resumableRelativePath": "testdocument.pdf",
"resumableTotalChunks": "1" }""")).asJSON
.bodyPart(RawFileBodyPart("file", "testdocument.pdf")
.fileName("testdocument.pdf")
.transferEncoding("binary")).asMultipartForm)
但是我还没有成功。对前进有什么建议吗?
如果它对其他人有帮助,我试图在 Gatling 已经这样做时设置自定义边界。这是解决我的问题的方法:
.exec(http("Post document: upload the file")
.post("/api/data/files?revisionId=${documentRevisionId}&uploadType=read_only_file&fileType=application%2Fpdf&fileName=testdocument.pdf&fileSize=10080©ToEditable=true") // ensure testdocument.pdf is in user-files/bodies
.headers(Headers.headers_6)
.formParamMap(Map(
"resumableChunkNumber" -> "1",
"resumableChunkSize" -> "1048576",
"resumableCurrentChunkSize" -> "10080",
"resumableTotalSize" -> "10080",
"resumableType" -> "application/pdf",
"resumableIdentifier" -> "${documentUuid}",
"resumableFilename" -> "testdocument.pdf",
"resumableRelativePath" -> "testdocument.pdf",
"resumableTotalChunks" -> "1"))
.bodyPart(RawFileBodyPart("file", "testdocument.pdf")
.fileName("testdocument.pdf")
.transferEncoding("binary")).asMultipartForm)
虽然@Chuck的回答是正确的,但是极简代码会像
一样简单
http("Create contentV2")
.post("/some/path")
.header("Content-Type", "multipart/form-data")
.bodyPart(StringBodyPart("changeRequest", changeRequest)).asMultipartForm
.bodyPart(StringBodyPart("payload", ElFileBody(filename))).asMultipartForm
.check(status is 201)
现在我们可以用多种东西代替 StringBodyPart,例如 RawFileBodyPart、ByteArrayBodyPart 等。更多信息 here
我 运行 遇到了 pdf 文件 POST 的问题。在 Gatling 2.1.7 中录制 HAR 文件后,这是我所拥有的:
.exec(http("request_10")
.post("/api/data/files?revisionId=e9af2c93-d8df-4424-b307-df4c4abbaad1&uploadType=read_only_file&fileType=application%2Fpdf&fileName=testdocument.pdf&fileSize=10080©ToEditable=true")
.headers(Map(
"Accept-Encoding" -> "gzip, deflate",
"Content-Type" -> "multipart/form-data; boundary=----WebKitFormBoundaryawCJ4mjL1imiO7Ye"
"Origin" -> url))
.body(RawFileBody("RecordedSimulation_0010_request.txt")))
与RecordedSimulation_0010_request.txt
的内容:
------WebKitFormBoundaryawCJ4mjL1imiO7Ye
Content-Disposition: form-data; name="resumableChunkNumber"
1
------WebKitFormBoundaryawCJ4mjL1imiO7Ye
Content-Disposition: form-data; name="resumableChunkSize"
1048576
------WebKitFormBoundaryawCJ4mjL1imiO7Ye
Content-Disposition: form-data; name="resumableCurrentChunkSize"
10080
------WebKitFormBoundaryawCJ4mjL1imiO7Ye
Content-Disposition: form-data; name="resumableTotalSize"
10080
------WebKitFormBoundaryawCJ4mjL1imiO7Ye
Content-Disposition: form-data; name="resumableType"
application/pdf
------WebKitFormBoundaryawCJ4mjL1imiO7Ye
Content-Disposition: form-data; name="resumableIdentifier"
66dc65bf-265d-4363-96fd-7fc13f8ceda4
------WebKitFormBoundaryawCJ4mjL1imiO7Ye
Content-Disposition: form-data; name="resumableFilename"
testdocument.pdf
------WebKitFormBoundaryawCJ4mjL1imiO7Ye
Content-Disposition: form-data; name="resumableRelativePath"
testdocument.pdf
------WebKitFormBoundaryawCJ4mjL1imiO7Ye
Content-Disposition: form-data; name="resumableTotalChunks"
1
------WebKitFormBoundaryawCJ4mjL1imiO7Ye
Content-Disposition: form-data; name="file"; filename="blob"
Content-Type: application/octet-stream
------WebKitFormBoundaryawCJ4mjL1imiO7Ye--
当我尝试播放它时这不起作用(可能是由于唯一 ID),所以(在 bodies 文件夹中粘贴 testdocument.pdf
之后)我做了以下操作:
val documentFeeder = Iterator.continually(Map(
"documentBoundary" -> (Random.alphanumeric.take(16).mkString),
"documentUuid" -> ((Random.alphanumeric.take(8).mkString + "-" +
Random.alphanumeric.take(4).mkString + "-" +
Random.alphanumeric.take(4).mkString + "-" +
Random.alphanumeric.take(4).mkString + "-" +
Random.alphanumeric.take(12).mkString).toLowerCase)))
...
.feed(documentFeeder)
// a previous exec POST with a check to grab the documentRevisionId
.exec(http("Post document: upload the file")
.post("/api/data/files")
.queryParamMap(Map(
"revisionId" -> "${documentRevisionId}",
"uploadType" -> "read_only_file",
"fileType" -> "application%2Fpdf",
"fileName" -> "testdocument.pdf",
"fileSize" -> "10080",
"copyToEditable" -> "true"))
.headers(Map(
"Accept-Encoding" -> "gzip, deflate",
"Content-Type" -> "multipart/form-data; boundary=----WebKitFormBoundary${documentBoundary}"
"Origin" -> url))
.body(StringBody("""------WebKitFormBoundary${documentBoundary}
Content-Disposition: form-data; name="resumableChunkNumber"
1
------WebKitFormBoundary${documentBoundary}
Content-Disposition: form-data; name="resumableChunkSize"
1048576
------WebKitFormBoundary${documentBoundary}
Content-Disposition: form-data; name="resumableCurrentChunkSize"
10080
------WebKitFormBoundary${documentBoundary}
Content-Disposition: form-data; name="resumableTotalSize"
10080
------WebKitFormBoundary${documentBoundary}
Content-Disposition: form-data; name="resumableType"
application/pdf
------WebKitFormBoundary${documentBoundary}
Content-Disposition: form-data; name="resumableIdentifier"
${documentUuid}
------WebKitFormBoundary${documentBoundary}
Content-Disposition: form-data; name="resumableFilename"
testdocument.pdf
------WebKitFormBoundary${documentBoundary}
Content-Disposition: form-data; name="resumableRelativePath"
testdocument.pdf
------WebKitFormBoundary${documentBoundary}
Content-Disposition: form-data; name="resumableTotalChunks"
1
------WebKitFormBoundary${documentBoundary}
Content-Disposition: form-data; name="file"; filename="blob"
Content-Type: application/octet-stream
Content-Transfer-Encoding: BINARY
------WebKitFormBoundary${documentBoundary}--""")))
最后,这是我们应用程序中 POST 虚拟数据的 Node.js 代码(我使用它作为参考,因为我知道它有效):
var resumableData = {
resumableChunkNumber: 1,
resumableChunkSize: 1048576,
resumableCurrentChunkSize: file.size,
resumableTotalSize: file.size,
resumableType: guessBestMimeType(file.name, file.type),
resumableIdentifier: genUuid(),
resumableFilename: file.name,
resumableRelativePath: file.name,
resumableTotalChunks:1
};
var boundaryKey = Math.random().toString(16); // random string
// the header for the one and only part (need to use CRLF here)
var resumableBody = '';
for(var resumablePart in resumableData){
if(resumableData.hasOwnProperty(resumablePart)){
resumableBody +=
'--' + boundaryKey + '\r\n' +
'Content-Disposition: form-data; name="' + resumablePart + '"\r\n\r\n' +
resumableData[resumablePart] + '\r\n';
}
}
resumableBody +=
'--' + boundaryKey + '\r\n' +
'Content-Disposition: form-data; name="file"; filename="blob"\r\n' +
// use your file's mime type here, if known
'Content-Type: application/octet-stream\r\n' +
'Content-Transfer-Encoding: BINARY\r\n\r\n';
var resumableEnd = '\r\n--' + boundaryKey + '--';
var request = https.request({
method : 'POST',
host : config.API_HOST + config.BASE_URL,
port : config.API_PORT,
path : generateUrl(documentRevision, file, fileType, convertEditable, copyToEditable),
headers : {
'Content-Type': 'multipart/form-data; boundary='+boundaryKey,
'Content-Length' : file.size + Buffer.byteLength(resumableBody + resumableEnd, 'utf-8')
}
}, function (response) {
var data = '';
response.on('data', function(chunk) {
data += chunk.toString();
});
response.on('end', function() {
resolve(JSON.parse(data));
});
response.on('error', function(err){
console.error(err);
reject(err);
});
});
request.write(resumableBody);
fs.createReadStream(file.path, { bufferSize: 4 * 1024})
.on('end', function() {
request.end(resumableEnd);
})
.pipe(request, { end: false });
几天来我一直在努力解决这个问题,这是我第一次涉足 Scala 和 Gatling。我在这里缺少什么才能得到这个 POST?
一个突出的问题(虽然它可能是一个转移注意力的问题)是我的应用程序在 header 中使用了 Content-Length
- 这是否有必要,因为 Gatling 已经省略了它?如果有必要,我是否可以在不硬编码的情况下将数字插入 Gatling 中?
编辑
阅读这篇文章后 post 我尝试了以下方法:
.exec(http("test post")
.post("/api/data/files")
.headers(Headers.headers_6)
.formParamMap(Map(
"revisionId" -> "${documentRevisionId}",
"uploadType" -> "read_only_file",
"fileType" -> "application%2Fpdf",
"fileName" -> "testdocument.pdf",
"fileSize" -> "10080",
"copyToEditable" -> "true"))
.bodyPart(StringBodyPart("""{ "resumableChunkNumber": "1",
"resumableChunkSize": "1048576",
"resumableCurrentChunkSize": "10080",
"resumableTotalSize": "10080",
"resumableType": "application/pdf",
"resumableIdentifier": "${documentUuid}",
"resumableFilename": "testdocument.pdf",
"resumableRelativePath": "testdocument.pdf",
"resumableTotalChunks": "1" }""")).asJSON
.bodyPart(RawFileBodyPart("file", "testdocument.pdf")
.fileName("testdocument.pdf")
.transferEncoding("binary")).asMultipartForm)
但是我还没有成功。对前进有什么建议吗?
如果它对其他人有帮助,我试图在 Gatling 已经这样做时设置自定义边界。这是解决我的问题的方法:
.exec(http("Post document: upload the file")
.post("/api/data/files?revisionId=${documentRevisionId}&uploadType=read_only_file&fileType=application%2Fpdf&fileName=testdocument.pdf&fileSize=10080©ToEditable=true") // ensure testdocument.pdf is in user-files/bodies
.headers(Headers.headers_6)
.formParamMap(Map(
"resumableChunkNumber" -> "1",
"resumableChunkSize" -> "1048576",
"resumableCurrentChunkSize" -> "10080",
"resumableTotalSize" -> "10080",
"resumableType" -> "application/pdf",
"resumableIdentifier" -> "${documentUuid}",
"resumableFilename" -> "testdocument.pdf",
"resumableRelativePath" -> "testdocument.pdf",
"resumableTotalChunks" -> "1"))
.bodyPart(RawFileBodyPart("file", "testdocument.pdf")
.fileName("testdocument.pdf")
.transferEncoding("binary")).asMultipartForm)
虽然@Chuck的回答是正确的,但是极简代码会像
一样简单 http("Create contentV2")
.post("/some/path")
.header("Content-Type", "multipart/form-data")
.bodyPart(StringBodyPart("changeRequest", changeRequest)).asMultipartForm
.bodyPart(StringBodyPart("payload", ElFileBody(filename))).asMultipartForm
.check(status is 201)
现在我们可以用多种东西代替 StringBodyPart,例如 RawFileBodyPart、ByteArrayBodyPart 等。更多信息 here