7

In our App we request a stream of information with Server-Sent Events. For this we use the small Library IKEventSource Which under the hood uses Foundation.URLSession.

This Information is sent in small JSON Packages For example:

{"type":"update","data":{"id":"1234","name":"someName","someOtherField":33,"size":"20","someAddress":"Awesome Street 111"}}

Now this works pretty good for us, but we noticed that sometimes we get the following Error:

Error Domain=kCFErrorDomainCFNetwork Code=303 "(null)" UserInfo={NSErrorPeerAddressKey=<CFData 0x610000a84510 [0x103ccbe40]>{length = 16, capacity = 16, bytes = 0x100201bbb9131fbd0000000000000000}, _kCFStreamErrorCodeKey=-2201, _kCFStreamErrorDomainKey=4}

Which i understand is a kCFErrorHTTPParseFailure The String it tries to Parse seems to be just a cut of JSON Package like:

{"type":"update","data":{"id":"1234","name":"so

Our current Understanding is, that URLSession is buffering the Data and sometimes it will fill up and the last part will be cut of. We could reproduce this in the Terminal using curl http://our.service.com and see a working example with curl -N http://our.service.com

Does somebody know how to add this option to URLSession, URLSessionConfiguration or URLSessionTask. Or does anybody have another explanation or Solution (maybe Server side) With this error our users sometimes miss updates to the Data and we think its the reason for some otherwise unexplainable Feedbacks.

BTW we have the same problem in our corresponding Android app

Peter Schumacher
  • 441
  • 4
  • 19

1 Answers1

1

I had the same error, without using any library, just by using my own implementation with URLSession.

I solved it by adding the HTTP header Accept: text/event-stream.

    var request = URLRequest(url: url)
    request.setValue("text/event-stream", forHTTPHeaderField: "Accept")
Guillaume
  • 4,331
  • 2
  • 28
  • 31