faunadb-csharp icon indicating copy to clipboard operation
faunadb-csharp copied to clipboard

Blazor WASM : http calls to FaunaDb blocked by CORS policy

Open Toine-db opened this issue 4 years ago • 1 comments

This library isn't working on Blazor WASM because of this 'x-query-timeout' header usage.

All HTTPClient calls get exceptions like: Access to fetch at 'https://db.fauna.com/' from origin 'https://.....' has been blocked by CORS policy: Request header field x-query-timeout is not allowed by Access-Control-Allow-Headers in preflight response.

Until this issue is solved on the FaunaDB cloud side you can use the following workaround: Remove the 'x-query-timeout' header in the request.

		public class MessageHandler : DelegatingHandler
		{
			public MessageHandler(HttpClientHandler httpClientHandler) :
				base(httpClientHandler) 
			{  }

			protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
			{
				// Dont use header 'X-Query-Timeout' for CORS policy
				request.Headers.Remove("X-Query-Timeout");
				return base.SendAsync(request, cancellationToken);
			}
		}
		public async void ContactFaunaDb()
		{
			var httpClient = new HttpClient(new MessageHandler(new HttpClientHandler()));
			var faunaDb = new FaunaClient(endpoint: Endpoint, secret: Secret, httpClient: httpClient);

			var rawIndexValues = await faunaDb.Query(Paginate(Match(Index("some_index"))));
			/// etc....
		}

Toine-db avatar Jun 24 '20 12:06 Toine-db

Thanks for bringing this up. We are going to tackle this really soon.

evbruno avatar Jun 24 '20 15:06 evbruno