加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – 加特林的POST请求

发布时间:2020-12-16 10:05:22 所属栏目:安全 来源:网络整理
导读:我遇到了pdf文件的POST问题.在Gatling 2.1.7中录制HAR文件之后,这就是我所拥有的: .exec(http("request_10") .post("/api/data/files?revisionId=e9af2c93-d8df-4424-b307-df4c4abbaad1uploadType=read_only_filefileType=application%2FpdffileName=testdo
我遇到了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&copyToEditable=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),这不起作用,所以(在body文件夹中粘贴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,"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 + 'rn' +
                'Content-Disposition: form-data; name="' + resumablePart + '"rnrn' +
                resumableData[resumablePart] + 'rn';
        }
    }
    resumableBody +=
        '--' + boundaryKey + 'rn' +
        'Content-Disposition: form-data; name="file"; filename="blob"rn' +
            // use your file's mime type here,if known
        'Content-Type: application/octet-streamrn' +
        'Content-Transfer-Encoding: BINARYrnrn';

    var resumableEnd = 'rn--' + 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.我在这里错过了什么来获得这个帖子?

一个问题(虽然它可能是一个红色的鲱鱼)脱颖而出是我的应用程序在标题中使用Content-Length – 这是必要的,因为Gatling省略了它?如果有必要,我能否在没有硬编码的情况下将数字插入加特林?

编辑

看完这个post后,我尝试了以下方法:

.exec(http("test post")
            .post("/api/data/files")
            .headers(Headers.headers_6)
            .formParamMap(Map(
                "revisionId" -> "${documentRevisionId}","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)

但我还没有成功.关于前进的任何建议?

解决方法

如果它帮助其他人,我试图设置一个自定义边界,当加特林已经这样做.这是解决我的问题的原因:

.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&copyToEditable=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)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读