Is there a way to get the response headers from a JsonServiceClient post call?
My response object is of type Stream and contains a memorystream (for a file to return). The file name is in the Content-Disposition header so before I try to present this file in the browser (vue) I want to set the file name to the value in the header.
For the TypeScript JsonServiceClient
in servicestack-client
library you can access headers in similar way, JavaScript included, much like other languages, you can use a responseFilter
which can be registered on the client using something like:
let fileName = null;
client.responseFilter = res => {
fileName = res.headers.get("content-disposition");
console.log(fileName)
}
let res = client.get(new DownloadFile())
Note that headers will be limited for CORS requests due to browser restrictions by default.
Ok, tried that, didn’t work. Probably something with the CORS, but the network console in the browser does show the header values, but the res.headers value are always empty.
There are a lot of limitations around CORS and can be frustrating to use but the key piece of info in the specs is the following from the CORS safelisted response header
Additional headers can be added to the safelist using Access-Control-Expose-Headers.
Addition header information is allowed if you explicitly set it in the response header of Access-Control-Expose-Headers
. This is set using the exposeHeaders
option in the CorsFeature
plugin. For example,
Plugins.Add(new CorsFeature(allowOriginWhitelist:new[]{
"http://localhost:5000",
"http://localhost:3000",
"https://localhost:5001",
"https://" + Environment.GetEnvironmentVariable("DEPLOY_CDN")
}, allowCredentials:true,
exposeHeaders: "Content-Disposition"));
This shouldn’t require a pre-flight request and you should be able to do a standard get
and extract the response headers the same way as above.
Note, you also limited in the way to can iterate or extract values from the .headers
object. You can either use the forEach
in Typescript from response.headers.forEach
or use headers.entries(). For getting a single value .get('key')
will work but array access like headers['key']
will result in undefined.