0

I have an ASP.Net Core web api that accepts bearer token (jwt) and I send this token in my Flutter with Android emulator without any problem with this code :

final response = await get(url, headers: {
      'Content-Type': 'application/json',
      'Accept': 'application/json',
      'Authorization': 'Bearer $token',
    });

But when I run this in Chrome browser the api response is : 405 Method Not Allowed .

What's the problem ?

UPDATE :

According to @anirudh comment I have enabled CROS in my web Api and problem have changed . Now the response is : 204 No Content

3 Answers 3

5

use http plugin

And do http.get and not just get

import 'package:http/http.dart' as http;

final response = await http.get(url, headers: {
      'Content-Type': 'application/json',
      'Accept': 'application/json',
      'Authorization': 'Bearer $token',
    });
4
  • @mmsh 405 occurs due to wrong request as far as I know. Pls try changing get to post. Also recheck your curl from this
    – anirudh
    May 15, 2021 at 6:46
  • Error: XMLHttpRequest error . if the problem is get or post why I don't have any problem in android with exactly same code ?
    – mm sh
    May 15, 2021 at 6:49
  • @mmsh Check this answer
    – anirudh
    May 15, 2021 at 6:50
  • @mmsh for 204 check this
    – anirudh
    May 15, 2021 at 11:53
1

Thanks to @anirudh I solved my problem

First defining a variable :

readonly string MyAllowSpecificOrigins = "_myAllowSpecificOrigins";

Then write these codes in my api in ConfigureServices :

services.AddCors(options =>
    {
        options.AddPolicy(name: MyAllowSpecificOrigins,
            builder =>
                {
                    builder.WithOrigins("http://localhost") //Or my flutter web host
                        .AllowAnyMethod()
                        .AllowAnyHeader()
                        .SetIsOriginAllowed((host) => true);
                });
    });

and this line in Configure :

app.UseCors(MyAllowSpecificOrigins);
0
0
String token = await Candidate().getToken();
final response = await http.get(url, headers: {
  'Content-Type': 'application/json',
  'Accept': 'application/json',
  'Authorization': 'Bearer $token',
});
print('Token : ${token}');
print(response);
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.