spring-cloud-gateway icon indicating copy to clipboard operation
spring-cloud-gateway copied to clipboard

Gateway issue with image upload.

Open manojsinghsaun opened this issue 1 year ago • 4 comments

I have two services gateway and myfunction services. I am uploading image using angular js. But when request routing to myfunction services the Image file get removed from request. I have used customer filter to in both gateway and myfunction sevrices to check if the request content image file or not.



import java.io.IOException;
import java.util.Collection;
import java.util.Map;

import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.Part;

@Component
public class CustomFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        String contentType = request.getHeader(HttpHeaders.CONTENT_TYPE);
        Object parameterValue = request.getParameter("file");
        		
        		if(contentType!=null && request.getContentType().startsWith(MediaType.MULTIPART_FORM_DATA_VALUE)) {
                    Collection<Part> parts = request.getParts();
                    for (Part part : parts) {
                        // Check if the part is a file or a form field
                        if (part.getContentType() != null) {
                            // File part
                            System.out.println("File part name: " + part.getName());
                            System.out.println("File name: " + part.getSubmittedFileName());
                            // You can process the file here
                        } else {
                            // Form field part
                            System.out.println("Form field name: " + part.getName());
                            // You can process the form field here
                        }
                    }
        		}
                    
                

        

        // Check if the request contains a file
        boolean hasFile = request.getContentType() != null && request.getContentType().startsWith(MediaType.MULTIPART_FORM_DATA_VALUE);
        

      Map<String, String[]> parameterMap = request.getParameterMap();
        for (String paramName : parameterMap.keySet()) {
            Object paramValues = parameterMap.get(paramName);
            System.out.println("manoj" +paramName);
           // for (String paramValue : paramValues) {
           //     System.out.println("Parameter: " + paramName + ", Value: " + paramValue);
            }


        // Perform your validations based on file presence, content type, and parameter value
       // if (hasFile && contentType != null && contentType.equals(MediaType.APPLICATION_JSON_VALUE) && parameterValue != null) {
            // Proceed with the request
            filterChain.doFilter(request, response);
       // } else {
            // Reject the request with a 400 Bad Request response
        //    response.setStatus(HttpStatus.BAD_REQUEST.value());
          //  response.getWriter().write("Invalid request parameters");
          //  return;
       // }
    }
}

I can see the file when request is in gateway but it get missed when it come to myfunction services.

I am using oauth2 client in gateway and ouath2 resources server in myfunction services.

please can you tell me what is worng with the gateway.

one more point to hightlight When I use postman It works fine the file did lost.

manojsinghsaun avatar Apr 09 '24 15:04 manojsinghsaun

I have also check this link but here tere haven't touch the part file which is coming with my request. which filter do that.

manojsinghsaun avatar Apr 09 '24 15:04 manojsinghsaun

reading the parts reads the stream and it is, therefor, not available to send anymore.

spencergibb avatar Apr 10 '24 23:04 spencergibb

Can you explain in detail..I did not get what you are explaing

manojsinghsaun avatar Apr 11 '24 17:04 manojsinghsaun

Can you explain in detail..I did not get what you are explaing

I might be misunderstanding but it sounds like what Spencer is saying is that you're already reading the stream in your filter. Once the stream has been read it's not being sent to your controller.

joel16 avatar May 10 '24 01:05 joel16