0

I am looking for a way to be able to send a GET HTTP request form Json style in dart. Everywhere I find that people had problems with the POST but no document about the GET. the issue is that if I write http.get(Uri.parse(uri)) I can't put the body: (where the form would go). it only lets me write body when it is a POST. i want to be able to run the next code with GET. The Uri.parse (i dont know why, but its the only way that allow me to get de URL)

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert' as convert;

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  postTest() async {
    final uri = 'http://10.0.2.2:5000/qna/';

    http.Response response = await http.post(Uri.parse(uri), body: {
      'pregunta': 'contacto',
    });

    print(response.body);
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Flutter WebView '),
        ),
        body: Container(
          child: TextButton(onPressed: postTest, child: Text('Push me')),
        ),
      ),
    );
  }
}
fartem
  • 1,670
  • 1
  • 5
  • 12
  • `get` method support only two arguments: `url` and `headers`. You can find full method description in [Official documentation](https://pub.dev/documentation/http/latest/http/get.html). – fartem May 23 '21 at 19:14
  • You can't put the body in a GET because [GET is used only to retrieve remote data, and not to insert](https://stackoverflow.com/a/3477374/9997212). – enzo May 23 '21 at 19:15
  • ha sorry guy for be a newbie ... im learning :) . but i have an answer ... if i want to do a login , its mean that i need to do it as a POST request ? because i cant not send a form in GET request... but every mobile app do with POST ? im confuse – jlemosmusi May 23 '21 at 19:28

1 Answers1

0

you cant add body in get request. but you can pass the data in the query param. like this

postTest() async {
    final uri = 'http://10.0.2.2:5000/qna?pregunta=contacto';

    http.Response response = await http.get(Uri.parse(uri));

    print(response.body);
  }
  • ha sorry guy for be a newbie ... im learning :) . but i have an answer ... if i want to do a login , its mean that i need to do it as a POST request ? because i cant not send a form in GET request... but every mobile app do with POST ? im confuse – jlemosmusi May 23 '21 at 19:24
  • you have to use post for login. – Elnatal Debebe May 24 '21 at 05:46