Code Monkey home page Code Monkey logo

Comments (8)

fangpenlin avatar fangpenlin commented on September 26, 2024

Hey @pranavs18, can you give me a workable code example that allows me to reproduce the problem you describe here? It would be helpful for me to figure what's going on there for you :)

from ambassador.

pranavs18 avatar pranavs18 commented on September 26, 2024
     router["/image"] = JSONResponse(handler: {environ -> Any in

         let input = environ["swsgi.input"] as! SWSGIInput
         DataReader.read(input) { data in
             // get the image bytes from the request object
             funcABC(data)
             startResponse("200 OK", [])
             sendBody(Data("".utf8))
             sendBody(Data())
         }
         return ["Image processed":"1"]
         }

I am trying to use curl -v -X POST -F '[email protected]' http://localhost:8080/image . This would do the multi-part form-data image upload. The issue with the above code snippet is that when we run the above curl command, the data in the closure is empty. I believe, since it's a multi part form-data upload on the server side, I would need to extract the file field from the http request body object to get the bytes. How can I do this using embassy?

If I were to do this in swift in another http framework, I can perhaps do this like:

let content = request.formData?["file"]?.part.body 
// get bytes  

let d = Data(bytes: content)

from ambassador.

fangpenlin avatar fangpenlin commented on September 26, 2024

@pranavs18 JSONResponse will respond immediately after you return via return ["Image processed":"1"]. Then it will close the HTTP connection. DataReader.read(input) is an async operation, which means it will wait until new HTTP body data arrive. So when JSONResponse handler returns, DataReader.read won't get a chance to really read the data.

As if you want to read the complete HTTP body payload, you will need to wait until DataReader.read called the callback then send the response to peer. For JSONResponse, there's another async constructor you can use

handler: @escaping (_ environ: [String: Any], _ sendJSON: @escaping (Any) -> Void) -> Void

Like this, you can modify your code with the sendJSON for finishing up the response

     router["/image"] = JSONResponse(handler: { (environ, sendJSON) -> Any in

         let input = environ["swsgi.input"] as! SWSGIInput
         DataReader.read(input) { data in
             // get the image bytes from the request object
             funcABC(data)
             sendJSON(["Image processed":"1"])
         }
     }

from ambassador.

pranavs18 avatar pranavs18 commented on September 26, 2024

I did try this before and it gives me a compilation error at the handler:{ (: Unable to infer closure type in the current context. I am using swift 4.1

            router["/image"] = JSONResponse(handler: { (environ, sendJSON) -> Any in
            let input = environ["swsgi.input"] as! SWSGIInput
            
            DataReader.read(input) { data in
                funcABC(data)
                sendJSON(["Image processed":"1"])
            }
        })

from ambassador.

pranavs18 avatar pranavs18 commented on September 26, 2024

It works if I change Any to void and I do get the bytes of the image uploaded. But I wonder how do I extract the image content from there ?

This snippet compiles but the data passed in funcABC() expects an image but fails to find that. When I print data, it gives me the total bytes. How would I extract the file content from there ?

           router["/image"] = JSONResponse(handler: { (environ, sendJSON) -> Void in
           let input = environ["swsgi.input"] as! SWSGIInput
           
           DataReader.read(input) { data in
               funcABC(data)
               sendJSON(["Image processed":"1"])
           }
       })

from ambassador.

pranavs18 avatar pranavs18 commented on September 26, 2024

I actually figured out the problem.

When I used curl with --data-binary @temp.jpg , that worked like a charm. All the bytes were correctly received and processed.

When I used curl with -F "[email protected], that fails. And it is probably because, the extra string file= and the multipart form-data boundary bytes need to be extracted away I believe from the total bytes uploaded. In this case, the total bytes uploaded are much more than in the case of --data-binary.

May be, you could add support for processing multi-part form-data as well.

from ambassador.

sushant-here avatar sushant-here commented on September 26, 2024

@fangpenlin Are there any plans or a workaround for supporting multi-part form-data?

from ambassador.

divyadilip91 avatar divyadilip91 commented on September 26, 2024

Is the ambassador authorizer now allowing multipart form data parameters to be passed through?

from ambassador.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.