0

I want to send a http post request to the server (json format) and get a response back in json format using flutter and dart. i have a code on C#, which is works

        var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://mysite");
        httpWebRequest.ContentType = "application/json";
        httpWebRequest.Method = "POST";

        using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
        {
            string json = "{\"email\":\"no@no.ru\"," +
                          "\"password\":\"mypass\"," +"\"remember\":\"false\"}";

            streamWriter.Write(json);
        }

        var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            var result = streamReader.ReadToEnd();
            Console.ReadLine(result.ToString());
        }

and on php

        $data = array(
          'email'      => 'no@no.ru',
          'password'    => 'mypass',
          'remember'       => 'false'
        );
        $options = array(
          'http' => array(
            'method'  => 'POST',
            'content' => json_encode( $data ),
            'header'=>  "Content-Type: application/json\r\n" .
             "Accept: application/json\r\n"
            ),
            "ssl"=>array(
                "verify_peer"=>false,
                "verify_peer_name"=>false,
            )
        );
        $url='https://mysite';
        $context  = stream_context_create( $options );
        $result = file_get_contents( $url, false, $context );
        $response = json_decode($result);
        var_dump($response);

but on dart it doesn't works

        var body = "{\"email\":\"no@no.ru\"," +        "\"password\":\"mypass\"," +"\"remember\":\"false\"}";

var url =
Uri.https('mysite', '/mypath');
Map<String,String> headers = {
  'Content-type' : 'application/json',
  'Accept': 'application/json',

};

http.Response response = await http.post(
    url,body:body,headers: headers);
print(response.body);

pubpecs.yaml:

http: ^0.13.0

i think the following error is caused by cors,but I can't solve it on the server side, as i written here

Error: XMLHttpRequest error.
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/patch/core_patch.dart 906:28                get current
packages/http/src/browser_client.dart 71:22                                                                                                    <fn>
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/zone.dart 1613:54                                              runUnary
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 155:18                                        handleValue
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 707:44                                        handleValueCallback
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 736:13                                        _propagateToListeners
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 533:7                                         [_complete]
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/stream_pipe.dart 61:11                                         _cancelAndValue
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/stream.dart 1219:7                                             <fn>
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 324:14  _checkAndCall
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 329:39  dcall
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/html/dart2js/html_dart2js.dart 37307:58                              <fn>


at Object.createErrorWithStack (http://localhost:57471/dart_sdk.js:5356:12)
at Object._rethrow (http://localhost:57471/dart_sdk.js:39475:16)
at async._AsyncCallbackEntry.new.callback (http://localhost:57471/dart_sdk.js:39469:13)
at Object._microtaskLoop (http://localhost:57471/dart_sdk.js:39301:13)
at _startMicrotaskLoop (http://localhost:57471/dart_sdk.js:39307:13)
at http://localhost:57471/dart_sdk.js:34814:9
sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
Anatoly
  • 11
  • 2

1 Answers1

0

You have to escape your backslashes in your Json body, if that's the exact body you want to send:

 var body = "{\\"email\":\\"no@no.ru\\"," +        "\\"password\\":\\"mypass\\"," +"\\"remember\\":\\"false\\"}";
Scott Godfrey
  • 641
  • 5
  • 8
  • this code is doesn't works. If i print(body) i get the following string: {"email":"no@no.ru","password":"mypass","remember":"false"} – Anatoly Mar 04 '21 at 11:53
  • I didn't see it at first, but you are writing it as a stream of characters into the request in your C# class. Is this a curl request? – Scott Godfrey Mar 04 '21 at 12:44
  • in C # and php everything works for me, dart does not work – Anatoly Mar 04 '21 at 12:46
  • Can you answer my question? Because, I believe Dart http doesn't natively support curl requests. – Scott Godfrey Mar 04 '21 at 13:28
  • natively no, but dart has a curl library https://pub.dev/packages/curl. Here is sample, which doesn't works for me: https://stackoverflow.com/questions/63011704/flutter-curl-post-request – Anatoly Mar 04 '21 at 13:59
  • Then, why are you using the standard http client? Also, you encode the string for the body in your C3 class, but not in the dart class. – Scott Godfrey Mar 06 '21 at 17:01
  • I've tried it in different ways, but the error is the same. What client, besides the standard one, can I use? – Anatoly Mar 07 '21 at 11:20
  • The curl package has it's own request object that should be used. It's in the documentation. – Scott Godfrey Mar 09 '21 at 11:47