Comments (3)
all operations with Embassy needs to be done in async, you cannot call blocking thing in side the loop, will block it.
struct RedirectResponse: WebApp {
func urlRequest(_ environ: [String: Any]) -> URLRequest {
//reading environment variables and transforming header keys to their initial values....
var request = URLRequest(url: URL(string: "http://httpbin.org/post")!)
request.httpMethod = "POST"
return request
}
public func app(_ environ: [String : Any], startResponse: @escaping ((String, [(String, String)]) -> Swift.Void), sendBody: @escaping ((Data) -> Swift.Void)) {
let input = environ["swsgi.input"] as! SWSGIInput
DataReader.read(input) { incomingData in
var request = self.urlRequest(environ)
request.httpBody = incomingData
let task = URLSession.shared.dataTask(with: request) { (data, resp, error) in
startResponse("200 OK", [])
if let data = data {
sendBody(data)
}
sendBody(Data())
}
task.resume()
}
}
}
Here's simple example I did for trying to get what you want. and the outcome
$ curl -i -X POST -H "mobile-app: IOS-838" -H "Content-Type: application/json" -H "Accept-Encoding: gzip;q=1.0, compress;q=0.5" -H "Accept-Language: en-US;q=1.0, en;q=0.9" -H "User-Agent: Alamofire/4.2.0" -d "{\"country\":\"us\",\"locale\":\"en-US\",\"grant_type\":\"client_credentials\",\"client_id\":\"...\",\"client_secret\":\"...\",\"scope\":\"public\"}" "http://localhost:8080/auth/oauth2/client/access_token"
HTTP/1.1 200 OK
Connection: close
Server: Embassy
{
"args": {},
"data": "",
"files": {},
"form": {
"{\"country\":\"us\",\"locale\":\"en-US\",\"grant_type\":\"client_credentials\",\"client_id\":\"...\",\"client_secret\":\"...\",\"scope\":\"public\"}": ""
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "en-us",
"Content-Length": "124",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "xctest (unknown version) CFNetwork/807.2.14 Darwin/16.4.0 (x86_64)"
},
"json": null,
"origin": "xxxxx",
"url": "http://httpbin.org/post"
}
from ambassador.
@victorlin thanks for the answer, that's exactly what I did in the beginning, but requests were never finishing. Now I see that I was missing calling sendBody
with empty data (I put it in else branch).
But with that I have random crashes on SelectorEventLoop.swift:209 (EXC_BAD_INSTRUCTION) after request returns. Sometimes in happens in other places. Also trying to serialise response I got error "Garbage at end." from time to time.
Here is my code if you want to try it out: https://gist.github.com/ilyapuchka/c9c3efb405e30207e6dd3003df3ec4c9
from ambassador.
So I came back to using Ambassador on a new project and seems like those crashes were related to using stop
instead of stopAndWait
and probably not synchronising current thread with url session task.
from ambassador.
Related Issues (20)
- Response body gets cut off HOT 5
- Server returning previous router response on high memory load HOT 2
- Multi-part form upload returns empty data using DataReader HOT 8
- 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.