Search code examples
dartdart-html

Response headers in Dart BrowserClient


I have a code where I POST a request and the response code is 201 created with "Location": "URL" header of the new created resource.

final Response response = await _http.post(getUri(_url), body: plan);
return _getPlan(response.headers['location']);

Where _http is a BrowserClient from the package http-0.12.0+1. But the response.headers['Location'] is empty. The headers array itself contains only one element Content-Type. In Chrome developers tools I see all response headers including Location. What am I doing wrong? How can I access the response headers?


Solution

  • Thanks to Günter Zöchbauer for pointing me for the answer. As described at https://stackoverflow.com/a/39021128/1444083 the missing header is Access-Control-Expose-Headers (not Access-Control-Allow-Headers).

    As I use Dart Aqueduct on the server side I had to add this line to the Channel prepare() method

    class MyAppChannel extends ApplicationChannel {
      @override
      Future prepare() async {
        // ... some code
        CORSPolicy.defaultPolicy.exposedResponseHeaders.add('location');
      }
    }
    

    so the responses now contain Access-Control-Expose-Headers: location