Moya: Moya request timing out - works fine in Alamofire directly

I have an endpoint that’s timing out when using Moya.

getApiProvider().request(.getOrder(id: 4))
  .subscribe { i in
    print(i)
  }

No errors, just the following in the console:

2016-12-17 12:19:20.223132 TestApp[46055:1474324] [] nw_endpoint_flow_service_writes [3.1 176.34.225.3:80 ready socket-flow (satisfied)] Write request has 4294967295 frame count, 0 byte count

I don’t see any logs showing this particular request on the server, and other requests from Moya are working fine.

When I perform an identical request with Alamofire…

Alamofire.request(
  "http://localhost:3000/api/v1/assistants/order?order_id=4",
  method: HTTPMethod.get, 
  parameters: nil, 
  encoding: JSONEncoding.default, 
  headers: [ "Authorization" : "Bearer <token>" ]
)
.response(completionHandler: { response in
    print(JSON(data: response.data!))
})

…the request completes without issue.

I’m sure it’s likely something simple I’m overlooking - any ideas what may be causing this?

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 31 (17 by maintainers)

Most upvoted comments

@scottrhoyt Just tried your suggestion this morning - that fixed it!

let endpoint = Endpoint<ToshiAPI>(
    url: url,
    sampleResponseClosure: {.networkResponse(200, target.sampleData)},
    method: target.method,
    parameters: target.parameters,
    parameterEncoding: target.method == .get ? URLEncoding.default : JSONEncoding.default
)

Looks like there’s an issue with Alamofire timing out when sending GET requests using Alamofire with JSON in the body: https://github.com/Alamofire/Alamofire/issues/1530, https://github.com/Alamofire/Alamofire/issues/1819

If you guys think this use case is common enough then this conditional is fine; perhaps I could add a pointer in the documentation though.

Otherwise I’d be happy to discuss other ways of handling this automatically when using JSONEncoding 👍 (Although perhaps that’s an issue for Alamofire itself)

Thank you so much everyone for your help!

@jonlambert Updating your view model code to:

class WelcomeViewModel: NSObject {
    let provider: RxMoyaProvider<API>
    
    override init() {
        provider = RxMoyaProvider<API>(
            endpointClosure:  MoyaProvider.defaultEndpointMapping,
            requestClosure: MoyaProvider.defaultRequestMapping,
            stubClosure: MoyaProvider.neverStub,
            manager: RxMoyaProvider<API>.defaultAlamofireManager(),
            plugins: [NetworkLoggerPlugin(cURL: true)],
            trackInflights: true
        )
        
        super.init()
    }
    
    func download() -> Observable<Moya.Response> {
        return provider.request(.ping)
    }
}

Results in expected behavior:

2017-01-10 17:48:01.958: WelcomeViewController.swift:39 (viewDidLoad()) -> subscribed
["$ curl -i \\\n\t-H \"Accept-Language: en;q=1.0\" \\\n\t-H \"User-Agent: MoyaIssue844/1.0 (co.evada.MoyaIssue844; build:1; iOS 10.2.0) Alamofire/4.2.0\" \\\n\t-H \"Accept-Encoding: gzip;q=1.0, compress;q=0.5\" \\\n\t\"https://hookb.in/Z6mo35ag/?foo=bar\""]
[["Moya_Logger: [10/01/2017 17:48:16] Response: <NSHTTPURLResponse: 0x608000039320> { URL: https://hookb.in/Z6mo35ag/?foo=bar } { status code: 200, headers {\n    \"Access-Control-Allow-Origin\" = \"*\";\n    \"Cache-Control\" = \"private, no-cache, no-store, must-revalidate\";\n    \"Content-Length\" = 16;\n    \"Content-Type\" = \"application/json; charset=utf-8\";\n    Date = \"Tue, 10 Jan 2017 22:48:17 GMT\";\n    Expires = \"-1\";\n    Pragma = \"no-cache\";\n    Server = nginx;\n    \"Strict-Transport-Security\" = \"max-age=31536000; includeSubdomains; preload\";\n    \"access-control-allow-headers\" = \"Origin, X-Requested-With, Content-Type, Accept\";\n    \"access-control-allow-methods\" = \"GET, PUT, POST, DELETE\";\n    \"x-expires-at\" = \"Tue, 17 Jan 2017 16:25:02 GMT\";\n    \"x-ratelimit-limit\" = 1000;\n    \"x-ratelimit-remaining\" = 999;\n    \"x-ratelimit-reset\" = 3600;\n    \"x-ua-compatible\" = \"IE=Edge,chrome=1\";\n} }"]]
2017-01-10 17:48:18.035: WelcomeViewController.swift:39 (viewDidLoad()) -> Event next(Status Code: 200, Data Length: 16)
Status Code: 200, Data Length: 16
2017-01-10 17:48:18.035: WelcomeViewController.swift:39 (viewDidLoad()) -> Event completed
2017-01-10 17:48:18.035: WelcomeViewController.swift:39 (viewDidLoad()) -> isDisposed

@jonlambert Any chance you could create a public repo that reproduces the problem? I’d jump in and do some debugging.