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\":\"[email protected]\"," +
                          "\"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'      => '[email protected]',
          '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\":\"[email protected]\"," +        "\"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

1 Answer 1

0

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

 var body = "{\\"email\":\\"[email protected]\\"," +        "\\"password\\":\\"mypass\\"," +"\\"remember\\":\\"false\\"}";
Sign up to request clarification or add additional context in comments.

8 Comments

this code is doesn't works. If i print(body) i get the following string: {"email":"[email protected]","password":"mypass","remember":"false"}
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?
in C # and php everything works for me, dart does not work
Can you answer my question? Because, I believe Dart http doesn't natively support curl requests.
natively no, but dart has a curl library pub.dev/packages/curl. Here is sample, which doesn't works for me: stackoverflow.com/questions/63011704/flutter-curl-post-request
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.