How to post x-www-form-urlencoded in Flutter How to post x-www-form-urlencoded in Flutter dart dart

How to post x-www-form-urlencoded in Flutter


As you can see, I am getting a 307 error, and the problem does not come from the server, as it worked with Postman.

No, that's NOT necessarily the case. Look here:

MDN: 307 Temporary Redirect

In other words, Postman is following the redirect ... and your Flutter app isn't.

SUGGESTION: Try setting followRedirects to true:

https://api.flutter.dev/flutter/dart-io/HttpClientRequest/followRedirects.html


ADDITIONAL INFO:

  • The default value for request.followRedirects happens to be "true" anyway. It doesn't hurt to explicitly set it ... but it explains why the behavior didn't change.

  • Per this post:

The Dart HTTP client won't follow redirects for POSTs unless the response code is 303. It follows 302 redirects for GET or HEAD.

The correct way to handle redirects on POST requests is to manually implement an appropriate strategy for your use case:

  var response = await client.post(...);  if (response.statusCode == 301 || response.statusCode == 302) {    // send post request again if appropriate  }


For x-www-form-urlencoded parameters, just use this:

  Future<String> login(user, pass) async {   final response = await http.post(    Uri.parse('https:youraddress.com/api'),    headers: {     "Content-Type": "application/x-www-form-urlencoded",    },    encoding: Encoding.getByName('utf-8'),    body: {"title": "title"},   ); if (response.statusCode == 200) {    // If the server did return a 200 OK response,   // then parse the JSON. } else {    // If the server did not return a 200 OK response,   // then throw an exception. }}


If you are using http, you should add the below lines

Android -

android/app/src/main/AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" /><application        android:label="first_app"        android:usesCleartextTraffic="true" //this line        android:icon="@mipmap/ic_launcher">

iOS -

ios/Runner/info.plist

<key>NSAppTransportSecurity</key><dict>        <key>NSAllowsArbitraryLoads</key>        <true/></dict>

Be warned that you will need to have an explanation for Apple's review team when enabling this, otherwise, your app will get rejected on submission.

Uninstall the app and Reinstall again if you have the app already in the emulator when you add those lines to avoid confusions.

If you send HTTP GET request, you can use query parameters as follows:

QueryParameters

http://example.com/path/to/page?name=ferret&color=purple

  • The contents are encoded as query parameters in the URL

application/x-www-form- urlencoded

  • The contents are encoded as query parameters in the body of therequest instead of the URL.

  • The data is sent as a long query string. The query string containsname-value pairs separated by & character

POST example

flutter http package version - http: ^0.13.1

import 'package:http/http.dart' as httpClient;Future<dynamic> postData() async {    //Uri.https("192.168.1.30:5000", "/api/data")    //Uri.parse("your url");    final Uri uri = Uri.http("192.168.1.30:5000", "/api/data");    final response = await httpClient.post(      uri,      body: {        "name": "yourName",        "age": "yourAge"      },      headers: {        "Content-Type": "application/x-www-form-urlencoded",      },      encoding: Encoding.getByName('utf-8'),    );    return response.body;  }