'Use Url.http for GET query
In my browser it returns the correct json
http://localhost:8008/api/instsets/?genre=rock
Howeber in dart code
final response = await http.get(Uri.http('localhost:8008', 'api/instsets/?genre=rock'));
It returns 404 error.
Without query, it works,
final response = await http.get(Uri.http('localhost:8008', 'api/instsets'));
Where should I fix??
Solution 1:[1]
According to flutter documentation, the query
parameter should be an object and you should pass it to the function as the third argument. So in your case, the code will be:
final response = await http.get(Uri.http('localhost:8008', 'api/instsets', { genre: 'rock' }));
Solution 2:[2]
It is because it is on your localhost
localhost = 127.0.0.1
You have to point your local IP address instead of localhost
ex: http://192.168.1.101:8008 like this.
In case of laravel api,
php artisan serve --host=192.168.1.101
Solution 3:[3]
You included an extra /
before your query.
You had 'api/instsets/?genre=rock'
Instead of 'api/instsets?genre=rock'
, which (I think) is also the same result as using the query
object.
APIs (at least the ones I use) are sensitive to trailing /
s
e.g. if you created a test-route
, calling
api/v1/test-route
would return200
api/v1/test-route/
would return404
Solution 4:[4]
final response = await http.get(Uri.parse('http://localhost:8008/api/instsets/?genre=rock'));
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | xkcm |
Solution 2 | KpStar |
Solution 3 | daddy7860 |
Solution 4 | DiyorbekDev |