Comments (8)
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.
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.
@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
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.
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.
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.
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.
@fangpenlin Are there any plans or a workaround for supporting multi-part form-data?
from ambassador.
Is the ambassador authorizer now allowing multipart form data parameters to be passed through?
from ambassador.
Related Issues (20)
- Response body gets cut off HOT 5
- Server returning previous router response on high memory load HOT 2
- Different response data and status codes HOT 1
- `4.0.4` tagged source has `4.0.3` version in Podspec HOT 1
- Get query string from a request HOT 2
- Running server in the different thread
- Performance issues when running tests on parallel HOT 2
- Similar endpoints HOT 4
- Router an API with parameters
- Router mixing similar routes HOT 2
- Supporting different states of a mocked response HOT 2
- New Release For Embassy
- Individual servers for parallel testing clones HOT 2
- Support many states api responses
- Delay response doesnt work as expected
- Retrieving request Headers through Router
- Providing path to json file instead of content of the json. HOT 1
- Fatal error with xcode 14 with Bad file descriptor
- Fatal error with iOS 16 in TCPSocket
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ambassador.