ClientException "Invalid response reason phrase" in http GET

1,008

Your server is serving slightly incorrect HTTP. Each header line should end with 0d 0a, not just 0a. Also, the separator between the last header and the body should be 0d 0a 0d 0a not 0a 0a.

Share:
1,008
Admin
Author by

Admin

Updated on December 08, 2022

Comments

  • Admin
    Admin over 1 year

    I am a new user to flutter/dart and am converting an Android native app to flutter.

    I am making a REST call to an IOT device (NodeMCU ESP8266) that I have written (in LUA) and am getting an Exception "Invalid response reason phrase" thrown in the IOClient send function in (io_client.dart).

    The rest call succeeds from every other device that I have tried from.

    The existing code uses RetroFit and works fine. Postman from my Linux PC is Ok, likewise chrome and firefox. Chrome from the Samsung device is also Ok.

    If I change to serving out the same data from json-server (npm package) on my Linux PC then the get succeeds, so it is definitely something to do with what/how I am serving out the response from the ESP8266. Below is a Wireshark trace of the call

    GET /pins HTTP/1.1
    user-agent: Dart/2.1 (dart:io)
    accept-encoding: gzip
    content-length: 0
    host: 192.168.1.78:8080
    
    HTTP/1.1 200 OK 
    Connection: close
    Content-Length: 230
    Content-Type: application/json; charset=utf-8
    
    [{"DeactivationTime":0,"OffTime":0,"Name":"Left Deck","MaxOnTime":888,"Pin":1,"ActivationTime":0,"IsActive":false},{"DeactivationTime":0,"OffTime":0,"Name":"Right Deck","MaxOnTime":888,"Pin":2,"ActivationTime":0,"IsActive":false}]
    

    the following is the most simplified test that I could do

    String url = 'http://192.168.1.78:8080/pins';
    try {
      http.get(Uri.parse(url)).then((result) {
        debugPrint(result.body.toString());
      });
     } catch (error) {
        debugPrint(error.toString());
     }
    

    I get the same error even with a 404 response. When talking to json-server the debug out is the json object as expected. I have played around with headers to match what is being returned by the json-server to no avail. Also the catch above never gets hit.

    Just for clarification here is the dump from Wireshark as hex

    00000000  47 45 54 20 2f 70 69 6e  73 20 48 54 54 50 2f 31   GET /pin s HTTP/1
    00000010  2e 31 0d 0a 75 73 65 72  2d 61 67 65 6e 74 3a 20   .1..user -agent: 
    00000020  44 61 72 74 2f 32 2e 31  20 28 64 61 72 74 3a 69   Dart/2.1  (dart:i
    00000030  6f 29 0d 0a 61 63 63 65  70 74 2d 65 6e 63 6f 64   o)..acce pt-encod
    00000040  69 6e 67 3a 20 67 7a 69  70 0d 0a 63 6f 6e 74 65   ing: gzi p..conte
    00000050  6e 74 2d 6c 65 6e 67 74  68 3a 20 30 0d 0a 68 6f   nt-lengt h: 0..ho
    00000060  73 74 3a 20 31 39 32 2e  31 36 38 2e 31 2e 37 38   st: 192. 168.1.78
    00000070  3a 38 30 38 30 0d 0a 0d  0a                        :8080... .
        00000000  48 54 54 50 2f 31 2e 31  20 32 30 30 20 4f 4b 20   HTTP/1.1  200 OK 
        00000010  0a                                                 .
        00000011  43 6f 6e 6e 65 63 74 69  6f 6e 3a 20 63 6c 6f 73   Connecti on: clos
        00000021  65 0a                                              e.
        00000023  43 6f 6e 74 65 6e 74 2d  4c 65 6e 67 74 68 3a 20   Content- Length: 
        00000033  32 33 30 0a                                        230.
        00000037  43 6f 6e 74 65 6e 74 2d  54 79 70 65 3a 20 61 70   Content- Type: ap
        00000047  70 6c 69 63 61 74 69 6f  6e 2f 6a 73 6f 6e 3b 20   plicatio n/json; 
        00000057  63 68 61 72 73 65 74 3d  75 74 66 2d 38 0a 0a      charset= utf-8..
        00000066  5b 7b 22 44 65 61 63 74  69 76 61 74 69 6f 6e 54   [{"Deact ivationT
        00000076  69 6d 65 22 3a 30 2c 22  4f 66 66 54 69 6d 65 22   ime":0," OffTime"
        00000086  3a 30 2c 22 4e 61 6d 65  22 3a 22 4c 65 66 74 20   :0,"Name ":"Left 
        00000096  44 65 63 6b 22 2c 22 4d  61 78 4f 6e 54 69 6d 65   Deck","M axOnTime
        000000A6  22 3a 38 38 38 2c 22 50  69 6e 22 3a 31 2c 22 41   ":888,"P in":1,"A
        000000B6  63 74 69 76 61 74 69 6f  6e 54 69 6d 65 22 3a 30   ctivatio nTime":0
        000000C6  2c 22 49 73 41 63 74 69  76 65 22 3a 66 61 6c 73   ,"IsActi ve":fals
        000000D6  65 7d 2c 7b 22 44 65 61  63 74 69 76 61 74 69 6f   e},{"Dea ctivatio
        000000E6  6e 54 69 6d 65 22 3a 30  2c 22 4f 66 66 54 69 6d   nTime":0 ,"OffTim
        000000F6  65 22 3a 30 2c 22 4e 61  6d 65 22 3a 22 52 69 67   e":0,"Na me":"Rig
        00000106  68 74 20 44 65 63 6b 22  2c 22 4d 61 78 4f 6e 54   ht Deck" ,"MaxOnT
        00000116  69 6d 65 22 3a 38 38 38  2c 22 50 69 6e 22 3a 32   ime":888 ,"Pin":2
        00000126  2c 22 41 63 74 69 76 61  74 69 6f 6e 54 69 6d 65   ,"Activa tionTime
        00000136  22 3a 30 2c 22 49 73 41  63 74 69 76 65 22 3a 66   ":0,"IsA ctive":f
        00000146  61 6c 73 65 7d 5d                                  alse}]