【问题标题】:http-builder in groovy uploading multiple files in servergroovy中的http-builder在服务器中上传多个文件
【发布时间】:2023-03-12 15:55:02
【问题描述】:

我正在尝试使用 http builder-groovy 在服务器中上传多个文件。以下代码不起作用。出现错误“请求实体太大”。我已导入所有包并定义了所有变量。

使用 restclient-groovy 有什么替代方法吗?

谁能给出原因?

提前致谢。

def file = new File("resources/IMG.JPG")
def file1 = new File("resources/aa.json")

http = new HTTPBuilder( url )

http.request (POST, JSON) { multipartRequest ->

      uri.path = '/server/upload'
      uri.query = [param1:value, param2:value, param3:value, param4:value]

      requestContentType = 'multipart/form-data'

      MultipartEntity mpe = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE)
      mpe.addPart( "jpeg", new FileBody(( File ) file , 'image/jpeg' ))
      mpe.addPart( "json", new FileBody(( File ) file1 , 'application/json' ))

      multipartRequest.setEntity(mpe)

      response.success = { resp, json->
          println "POST response status: ${resp.statusLine}"
          println "Query response: ${json}"
      }

      response.failure = {  resp ->
          println "POST response statusline: ${resp.statusLine}"
      }
}

【问题讨论】:

标签: groovy multipartform-data httpbuilder


【解决方案1】:

您实际上可以使用 RestAssured 框架轻松地形成多部分请求。

下面是一个例子,

@Test
public void multiExample()
{
    given().
        multiPart("image", new File("resources/a.jpg"), "image/jpeg").
        multiPart("lgdata", new File("resources/myfile.json"), "application/json").
    expect().
        body("result", is("OK")).
    when().
        post(url);

}

【讨论】:

    【解决方案2】:

    根据维基百科:

    413 请求实体太大

    请求大于服务器愿意或能够处理的。

    检查你的服务器

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-12
      • 2017-01-01
      • 1970-01-01
      • 2015-09-23
      • 1970-01-01
      • 2011-07-13
      • 2010-10-30
      • 1970-01-01
      相关资源
      最近更新 更多