dimanche 19 septembre 2021

Flutter dart web post request like python requests

Hi Hope someone can assist. I am a blind dev and am having some problems doing a post request with flutter dart webapp. If I use a python script to test if I can retireve the data I get it. How can I convert the python way to flutter dart. It will be a webapp or pwa so some of the libraries is not useable for me.

I tried sending the body in all different ways and included also headers. Some times I get a xml http request error and other times it just not compile. Below is the python code that works with the request and below it I will put my dart method.

    import requests, json
    s = requests.Session()
    s.verify = False 
    d_post = {"key": "long string", "start": "2021-01-01 0:00"}#optional "end":                 "2021-07-31"}
    rp = s.post("https://webAddress.co.za/pi_logging/log_retrieve.php", d_post)
    print(rp.status_code)
    print(rp.text)
    #d_out = json.loads(rp.text)
    #print(d_out)

This give the required output. Now below is one of my flutter dart attemps

    Future fetchData() async {
      final response = await http.post(Uri.parse("https://webAddres.co.za/pi_logging/log_retrieve.php"), //body: {"key": "longstring", "start": "2021-01-01 0:00"}, 
      headers: <String, String>{
        'Content-Type': 'application/json; charset=UTF-8',
      },
      body: jsonEncode(<String, String>{
    "key": "longstring", "start": "2021-01-01 0:00"
      }),
      );

        //headers: {
      //"Accept": "application/json",
      //"Access-Control-Allow-Origin": "*"
      //});

      print(response.statusCode);

The commented out peaces is also some of the code I tried to get a request return from the server but I do not even get a 200 response where with python script I get the 200 response and the json data requested. If anyone can assist or point me in the correct direction to solve this I will appreciate it. Thank you




Aucun commentaire:

Enregistrer un commentaire