简体   繁体   English

Azure 逻辑应用 http 发布 multipart/form-data 文件上传

[英]Azure logic app http post multipart/form-data file upload

I am trying to get call an api with the HTTP logic app within Azure我正在尝试使用 Azure 中的 HTTP 逻辑应用程序调用 api

I am able to make the call successful through post man.我能够通过邮递员成功拨打电话。 see my post man configuration看我的邮递员配置

邮递员配置

I can see the http code from postman as this, I am using this to make the logic app to be formatted similar to what post man has.我可以看到来自邮递员的 http 代码,我正在使用它来使逻辑应用程序的格式类似于邮递员所拥有的格式。

POST /dcma/rest/initiateOcrClassifyExtract HTTP/1.1

Host: godemo.ephesoft.com

Authorization: Basic NDU=??????

Cache-Control: no-cache

Postman-Token: 3baf23e7-6b46-a5f4-094b-3df1879bbe21

Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="000001.pdf"; filename="000001.pdf"
Content-Type: application/pdf


------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="batchClassIdentifier"

BC590

------WebKitFormBoundary7MA4YWxkTrZu0gW--

below is the actual logic app http configuration.下面是实际的逻辑应用 http 配置。

逻辑应用配置

the logs on the server show the below error服务器上的日志显示以下错误

    2017-09-07 20:12:51,784 [ajp-apr-8009-exec-3] ERROR org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/dcma].[DispatcherServlet]- Servlet.service() for servlet [DispatcherServlet] in context with path [/dcma] threw exception [Request processing failed; nested exception is org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nested exception is org.apache.commons.fileupload.FileUploadException: Header section has more than 10240 bytes (maybe it is not properly terminated)] with root cause
org.apache.commons.fileupload.MultipartStream$MalformedStreamException: Header section has more than 10240 bytes (maybe it is not properly terminated)
    at org.apache.commons.fileupload.MultipartStream.readHeaders(MultipartStream.java:541)
    at org.apache.commons.fileupload.FileUploadBase$FileItemIteratorImpl.findNextItem(FileUploadBase.java:999)
    at org.apache.commons.fileupload.FileUploadBase$FileItemIteratorImpl.<init>(FileUploadBase.java:965)
    at org.apache.commons.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:331)
    at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:351)
    at org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:126)
    at org.springframework.web.multipart.commons.CommonsMultipartResolver.parseRequest(CommonsMultipartResolver.java:158)
    at org.springframework.web.multipart.commons.CommonsMultipartResolver.resolveMultipart(CommonsMultipartResolver.java:142)
    at org.springframework.web.servlet.DispatcherServlet.checkMultipart(DispatcherServlet.java:1070)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:912)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at com.ephesoft.dcma.webapp.AuthenticationFilter.doFilter(AuthenticationFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at com.ephesoft.dcma.webapp.SessionTimeoutFilter.doFilter(SessionTimeoutFilter.java:43)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:230)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at com.ephesoft.dcma.webapp.HTTPHeaderFilter.doFilter(HTTPHeaderFilter.java:75)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:218)

From the log message you offered, I found that you used the apache.commons.fileupload library in the backend.从您提供的日志消息中,我发现您在后端使用了apache.commons.fileupload库。 So,I created a simple servlet web project which contains the core code as below with the apache.commons.fileupload library to process uploaded files.因此,我创建了一个简单的 servlet web 项目,其中包含如下核心代码和apache.commons.fileupload库来处理上传的文件。

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

@WebServlet("/HelloWorldByPostman")
public class HelloWorldByPostman extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public HelloWorldByPostman() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

//      String savePath = this.getServletContext().getRealPath("/WEB-INF/upload");

        boolean isMultipart = ServletFileUpload.isMultipartContent(request); 
        System.out.println(isMultipart);

        if(isMultipart){  
            FileItemFactory factory = new DiskFileItemFactory();  
            ServletFileUpload upload = new ServletFileUpload(factory);  
            List<FileItem> items = null;  
            try {
                items=upload.parseRequest(request);
                System.out.println(items.toString());  
            } catch (FileUploadException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }  
            Iterator<FileItem> iterator = items.iterator();  
            while(iterator.hasNext()){  
                FileItem item = iterator.next();  
                if(item.isFormField()){  
                    System.out.println("is txt........"+item.getFieldName());  
                }else{  
                    System.out.println("is file..........."+item.getFieldName());  
                }  
            }  
        }  

        response.getWriter().append("Served at: ").append("jaygong");
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

Then,I deployed the project to Azure Web App so that I can upload files via http request.然后,我将该项目部署到Azure Web App以便我可以通过 http 请求上传文件。

Postman:邮递员:

在此处输入图片说明

在此处输入图片说明

Result:结果: 在此处输入图片说明

Logic App:逻辑应用:

在此处输入图片说明

Result:结果:

在此处输入图片说明

When the action is post and Content-Type type is multipart/form-data , the browser will take the form to control unit segmentation, and for each part plus Content-Disposition (form-data or file), Content-Type (default is text/plain), name (name control) and other information, and add a boundary.当动作为post且 Content-Type 类型为multipart/form-data ,浏览器会以表单来控制单元分割,对于每个部分加上 Content-Disposition(表单数据或文件),Content-Type(默认为text/plain)、name(名称控件)等信息,并添加边界。

The Content-Type attribute is already included in Body part so you could remove the Content-Type setting in Header part and retry your post request. Content-Type属性已包含在Body部分中,因此您可以删除Header部分中的Content-Type设置Header试您的发布请求。

Hope it helps you.希望对你有帮助。

For multipart/form-data there is now an example in the official documentation:对于 multipart/form-data 现在官方文档中有一个示例:

https://docs.microsoft.com/bs-latn-ba/azure/connectors/connectors-native-http#content-with-multipartform-data-type https://docs.microsoft.com/bs-latn-ba/azure/connectors/connectors-native-http#content-with-multipartform-data-type

"body": {
   "$content-type": "multipart/form-data",
   "$multipart": [
      {
         "body": "<output-from-trigger-or-previous-action>",
         "headers": {
            "Content-Disposition": "form-data; name=file; filename=<file-name>"
         }
      }
   ]
}

And if there are other form fields to send, just add similar objects with correct form field names.如果还有其他表单字段要发送,只需添加具有正确表单字段名称的类似对象。

The Content-Disposition header will need escaped quotes for the name and filename field if you are not using the TriggerBody from a previous step.如果您没有使用上一步中的 TriggerBody,则 Content-Disposition 标头的名称文件名字段将需要转义引号。

"Content-Disposition": "form-data; name=\"file\"; filename=\"<file-name>\""

you need to pass the data like In body你需要像 In body 一样传递数据

{
  "$Content-Type": "application/x-www-form-urlencoded",
  "$formdata": [
    {
      "key": "grant_type",
      "value": "client_credentials"
    },
    {
      "key": "client_id",
      "value": "your clientid"
    },
    {
      "key": "client_secret",
      "value": "your client secret"
    }
  ]
}

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

相关问题 如何将GZIP内容编码应用于HTTP文件上传multipart / form-data POST - How can I apply GZIP content-encoding to an HTTP file upload multipart/form-data POST 与多部分/表单数据上传表单一起传递参数(Java Http 上传后) - Passing parameters along with a multipart/form-data upload form (Java Http Post Upload) 使用Camel上传多部分表单数据文件 - Multipart form-data file upload with Camel 如何使用Java Spring将带有上传文件的多部分/表单数据表单重新发送到其他服务器 - How to resend post multipart/form-data form with upload file to different server with Java Spring Java-使用远程文件(带有http://或file://协议)的multipart / form-data POST请求 - Java - multipart/form-data POST request using a remote file (with http:// or file:// protocols) 如何使用SpringMVC和MockMVC发布文件上载的multipart / form-data - How to Post multipart/form-data for a File Upload using SpringMVC and MockMVC 带有多部分/表单数据和身份验证的 Java 上传文件 - Java upload file with multipart / form-data and authentication 多部分/表格数据文件上传+泽西岛的其他参数 - multipart/form-data file upload + other parameters in Jersey 在Java中使用Restlet multipart / form-data上载文件 - Upload a file using Restlet multipart/form-data in java 使用 Feign 上传文件 - multipart/form-data - File Upload Using Feign - multipart/form-data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM