whitebear
whitebear

Reputation: 12423

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??

Upvotes: 1

Views: 451

Answers (4)

DiyorbekDev
DiyorbekDev

Reputation: 774

final response = await http.get(Uri.parse('http://localhost:8008/api/instsets/?genre=rock'));

Upvotes: 0

daddy7860
daddy7860

Reputation: 431

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 return 200
  • api/v1/test-route/ would return 404

Upvotes: 0

xkcm
xkcm

Reputation: 188

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' }));

Upvotes: 1

KpStar
KpStar

Reputation: 132

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

Upvotes: 0

Related Questions