简体   繁体   English

如何使用多部分表单正文模拟发布请求

[英]How to Mock Post request with multi part form body

I need to mock a post request with multipart form body as given below.我需要使用下面给出的多部分表单主体来模拟发布请求。 I tried with a regex pattern for the unique string in the body --Z5KfGboCdJvPl4VsEq74AeK7PZN4EyN, but it didn't work.我尝试对正文中的唯一字符串使用正则表达式模式--Z5KfGboCdJvPl4VsEq74AeK7PZN4EyN,但它没有用。 Can some one guide me on this?有人可以指导我吗?

BODY:身体:

"body":"--Z5KfGboCdJvPl4VsEq74AeK7PZN4EyN
Content-Disposition: form-data; name="dataset"
Content-Type: text/plain;charset=UTF-8
Content-Length: 14 drugs --Z5KfGboCdJvPl4VsEq74AeK7PZN4EyN
Content-Disposition: form-data; name="searchQuery"
Content-Type: text/plain;charset=UTF-8
Content-Length: 6 cancer --Z5KfGboCdJvPl4VsEq74AeK7PZN4EyN
Content-Disposition: form-data; name="queryLanguage"
Content-Type: text/plain;charset=UTF-8
Content-Length: 4 ssql --Z5KfGboCdJvPl4VsEq74AeK7PZN4EyN
Content-Disposition: form-data; name="returnFields"
Content-Type: text/plain;charset=UTF-8
Content-Length: 22 Id,name --Z5KfGboCdJvPl4VsEq74AeK7PZN4EyN
Content-Disposition: form-data; name="returnLimit"
Content-Type: text/plain;charset=UTF-8
Content-Length: 1 1 --Z5KfGboCdJvPl4VsEq74AeK7PZN4EyN
Content-Disposition: form-data; name="offset"
Content-Type: text/plain;charset=UTF-8
Content-Length: 1 0 --Z5KfGboCdJvPl4VsEq74AeK7PZN4EyN-- "

I tried like -我试过像 -

mockServerClient
.when(
request()
.withMethod("POST")
.withHeaders(
new Header("Content-Type", "multipart/form-data"))
.withPath("abc/xyz")
.withBody(**regex("[\n.\w\W]***
Content-Disposition: form-data; name="dataset"
Content-Type: text/plain;charset=UTF-8
Content-Length: 14 drugs [\n.\w\W]*
Content-Disposition: form-data; name="searchQuery"
Content-Type: text/plain;charset=UTF-8
Content-Length: 6 cancer [\n.\w\W]*
Content-Disposition: form-data; name="queryLanguage"
Content-Type: text/plain;charset=UTF-8
Content-Length: 4 ssql [\n.\w\W]*
Content-Disposition: form-data; name="returnFields"
Content-Type: text/plain;charset=UTF-8
Content-Length: 22 Id,name [\n.\w\W]*
Content-Disposition: form-data; name="returnLimit"
Content-Type: text/plain;charset=UTF-8
Content-Length: 1 1 [\n.\w\W]*
Content-Disposition: form-data; name="offset"
Content-Type: text/plain;charset=UTF-8
Content-Length: 1 0 [\n.\w\W]*-- "))
)

Try this regex:试试这个正则表达式:

^(--[\w]+)$\n(?:^Content-Disposition: form-data; name=\"\w+?\"$\n^Content-Type: text/plain;charset=UTF-8$\n^Content-Length: \d+? [\w,]+ \1(?:-- $|$\n))+

In your version I didn't see that you are escaping the double quotes " to a \" , wondering if the code compiles at all.在您的版本中,我没有看到您是 escaping 双引号"\" ,想知道代码是否可以编译。 Also the [\n.\w\W] seems to be a bit too generic.此外, [\n.\w\W]似乎有点过于笼统。

The above version is also generic, but in a different way:上面的版本也是通用的,但方式不同:

(--[\w]+) - this is a group that expects the border string. (--[\w]+) - 这是一个需要边框字符串的组。 In the rest of the expression the match it is "back referenced" as \1 , this means only requests with the same border over the whole request are accepted.在表达式的 rest 中,匹配它被“反向引用”为\1 ,这意味着只接受在整个请求上具有相同边界的请求。

A longer part is following for one of the parts between the borders.对于边界之间的部分之一,后面是较长的部分。 Please note that the expression does not care which parts are actually contained in the request.请注意,该表达式不关心请求中实际包含哪些部分。 If you need that, let me know then I can update the expression to accept just the given parts.如果您需要,请告诉我,然后我可以更新表达式以仅接受给定的部分。

@cyberbrain. @cyberbrain。 Thanks for looking into this.感谢您查看这个。

I am trying to mock post request with multipart - form data, where the post body looks like i have shown below (where the boundary value changes on every run).我正在尝试使用多部分表单数据模拟发布请求,其中帖子正文如下所示(每次运行时边界值都会发生变化)。 so, i am trying to use regex only for boundary part.Any pointers will be really helpful.所以,我试图将正则表达式仅用于边界部分。任何指针都会非常有帮助。

I tried the regex you have suggested but it didnt work.我尝试了您建议的正则表达式,但没有用。 Actual Request -实际要求 -

{
"method":"POST"
"path":"/abc/xyz"
"headers":{...}
"keepAlive":true
"secure":false
"body":"--y_My4VSoa71UaoRG6KF4DM_MZAP7AKDz54 Content-Disposition: form-data; name="dataset" Content-Type: text/plain;charset=UTF-8 Content-Length: 14 drug --y_My4VSoa71UaoRG6KF4DM_MZAP7AKDz54 Content-Disposition: form-data; name="searchQuery" Content-Type: text/plain;charset=UTF-8 Content-Length: 6 cancer --y_My4VSoa71UaoRG6KF4DM_MZAP7AKDz54 Content-Disposition: form-data; name="queryLanguage" Content-Type: text/plain;charset=UTF-8 Content-Length: 4 ssql --y_My4VSoa71UaoRG6KF4DM_MZAP7AKDz54 Content-Disposition: form-data; name="returnFields" Content-Type: text/plain;charset=UTF-8 Content-Length: 7 Id,Name --y_My4VSoa71UaoRG6KF4DM_MZAP7AKDz54 Content-Disposition: form-data; name="returnLimit" Content-Type: text/plain;charset=UTF-8 Content-Length: 1 1 --y_My4VSoa71UaoRG6KF4DM_MZAP7AKDz54 Content-Disposition: form-data; name="offset" Content-Type: text/plain;charset=UTF-8 Content-Length: 1 0 --y_My4VSoa71UaoRG6KF4DM_MZAP7AKDz54-- "
}

mock request i gave like this -我这样提出的模拟请求-

mockServerClient
.when(
      request()
        .withMethod("POST")
//      .withHeaders(
//         new Header("Content-Type", "multipart/form-data"))
        .withPath("/abc/xyz")
        .withBody(regex("(--[\\w]+) Content-Disposition: form-data; name=\"dataset\" Content-Type: text/plain;charset=UTF-8 Content-Length: 14 drug (--[\\w]+) Content-Disposition: form-data; name=\"searchQuery\" Content-Type: text/plain;charset=UTF-8 Content-Length: 6 cancer (--[\\w]+) Content-Disposition: form-data; name=\"queryLanguage\" Content-Type: text/plain;charset=UTF-8 Content-Length: 4 ssql (--[\\w]+) Content-Disposition: form-data; name=\"returnFields\" Content-Type: text/plain;charset=UTF-8 Content-Length: 7 Id,Name (--[\\w]+) Content-Disposition: form-data; name=\"returnLimit\" Content-Type: text/plain;charset=UTF-8 Content-Length: 1 1 (--[\\w]+) Content-Disposition: form-data; name=\"offset\" Content-Type: text/plain;charset=UTF-8 Content-Length: 1 0 (--[\\w]+)-- "))
)

**and the expectation set by Mock Server as- ** - **和 Mock Server 设定的期望为- ** -

"httpRequest":{
"method":"POST"
"path":"/abc/xyz"
"body":{
"type":"REGEX"
"regex":"(--[\w]+) Content-Disposition: form-data; name="dataset" Content-Type: text/plain;charset=UTF-8 Content-Length: 14 drug (--[\w]+) Content-Disposition: form-data; name="searchQuery" Content-Type: text/plain;charset=UTF-8 Content-Length: 6 cancer (--[\w]+) Content-Disposition: form-data; name="queryLanguage" Content-Type: text/plain;charset=UTF-8 Content-Length: 4 ssql (--[\w]+) Content-Disposition: form-data; name="returnFields" Content-Type: text/plain;charset=UTF-8 Content-Length: 7 Id,Name (--[\w]+) Content-Disposition: form-data; name="returnLimit" Content-Type: text/plain;charset=UTF-8 Content-Length: 1 1 (--[\w]+) Content-Disposition: form-data; name="offset" Content-Type: text/plain;charset=UTF-8 Content-Length: 1 0 (--[\w]+)-- "
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在请求请求的正文中使用带有多部分数据的齐射将图像上传到服务器 - Uploading image to server using volley with multi-part data in body of post request 如何确定多部分POST请求Java的文件大小? - How to determine the file size for multi-part POST request Java? 如何在 Java HttpURLConnection 中为多部分表单帖子禁用缓冲? - How to disable buffering in Java HttpURLConnection for multi-part form post? 如何使用 x-www-form-urlencoded 正文发送 post 请求 - How to send post request with x-www-form-urlencoded body MockRestServiceServer:如何使用正文模拟POST调用? - MockRestServiceServer: how to mock a POST call with a body? Spring Data Rest:如何使用请求正文发送多部分文件 - Spring Data Rest : how to send Multi-part file with request Body 如何接收请求正文数据以及多部分图像文件? - How to receive request body data along with multi-part image file? spring 模拟 mvc 执行发布请求正文丢失:错误 400 - spring mock mvc perform post request body is missing : error 400 如何形成 http 请求主体的 protobuf 资源部分并通过 dhc 客户端或 postman 测试它以获得宁静的服务 - How to form protobuf resource part of http request body and test it through dhc client or postman for restful services 如何在 POST 请求中传递 Headers 和 request body? - How to pass Headers and request body in POST request?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM