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

mm sh
  • 163
  • 1
  • 13

3 Answers3

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',
    });
anirudh
  • 1,367
  • 3
  • 16
  • thanx . I've changed my code as you wrote but still have that error in web only – mm sh May 15 '21 at 06:32
  • @mmsh can you send the error ? Maybe then I'll be able to help you – anirudh May 15 '21 at 06:42
  • @mmsh 405 occurs due to wrong request as far as I know. Pls try changing `get` to `post`. Also recheck your curl from [this](https://curl.trillworks.com/#dart) – anirudh May 15 '21 at 06: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 '21 at 06:49
  • @mmsh Check [this answer](https://stackoverflow.com/questions/60191683/xmlhttprequest-error-in-flutter-web-enabling-cors-aws-api-gateway) – anirudh May 15 '21 at 06:50
  • @mmsh ANY Luck? – anirudh May 15 '21 at 06:59
  • unfortunately no . this problem is in authentication step in web api . but that post soloution for response step – mm sh May 15 '21 at 07:28
  • @mmsh for 204 check [this](https://stackoverflow.com/questions/12807753/http-get-with-204-no-content-is-that-normal) – anirudh May 15 '21 at 11:53
  • 1
    thanx a lot . I solved my problem whit the link that you sent and wrote the solution . If you want you can write the soloution and I accept that for final solotion – mm sh May 16 '21 at 14:17
  • @mmsh , no you have solved it yourself, you can mark your answer as correct , it will definitely help someone in future :) – anirudh May 16 '21 at 19:23
  • Thank you a lot – mm sh May 17 '21 at 04:28
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);
mm sh
  • 163
  • 1
  • 13
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);
  • thanx , I have token and I haven't any problem with that in android . My problem is only in web – mm sh May 15 '21 at 06:33