Code Monkey home page Code Monkey logo

Comments (3)

fangpenlin avatar fangpenlin commented on September 26, 2024

@ilyapuchka

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.

ilyapuchka avatar ilyapuchka commented on September 26, 2024

@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.

ilyapuchka avatar ilyapuchka commented on September 26, 2024

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)

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.