Flutter - ERROR : HTTP 405 Method not allowed
3,252
Method not allowed might refer to wrong request method.
Try using POST instead of GET, at least I got a response from that using the mentioned URL.
Future<String> getData() async {
var response = await http.post(
Uri.encodeFull("http://finelistings.com/backend/apis/webbrands/"),
headers: {
"Accept": "application/json",
}
);
Map<String, dynamic> data = jsonDecode(response.body);
print(data);
}
Author by
hasib_ullah
Simplicity is prerequisite for reliability and I deliver both
Updated on December 14, 2022Comments
-
hasib_ullah over 1 year
I'm trying to read json file through an API that was given to me by a client, but It's giving HTTP error 405 saying method not allowed. Can anyone please tell what I'm doing wrong?
This api request snippet was given to me :
curl 'http://finelistings.com/backend/apis/webbrands/' -H 'Content-Type: application/json' -H 'Accept: application/json' --data-binary '{}' --compressed --insecure
Future<String> getData() async { var response = await http.get( Uri.encodeFull("http://finelistings.com/backend/apis/webbrands/"), headers: { "Accept": "application/json", } ); Map<String, String> data = jsonDecode(response.body); print(data); }
-
Praneeth Dhanushka Fernando over 4 yearsis the Curl call working correctly?
-
Richard Heap over 4 yearsThere's a great resource that converts curl commands to dart. As @Torkel noticed, you need to submit as a POST. You missed the part of the curl command that says
--data-binary...
which sends a body to the server with the request, which has to be done with a POST.
-
-
hasib_ullah over 4 yearsI tried this method but now I'm getting a new error on the line
Map<String, String> data = jsonDecode(response.body);
. Error:Exception has occurred. _TypeError (type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Map<String, String>')
-
Torkel Velure over 4 yearsTry using dynamic instead of string? It's what the error message says