简体   繁体   中英

Converting object to an encodable object failed due to 404

Receiving 404 error while writing the data in json format.

var response = await http.post(Uri.parse(url + "/api/register"),
              body: jsonEncode({
                "username": _username.text,
                "first_name": _fname.text,
                "last_name": _lname.text,
                "password": _password.text,
                "email": _email.text,
                "usertype": _userDropdown.toString(),
                "gender": _genderDropdown.toString(),`enter code here`
                "bloodgroup": _bloodDropdown.toString(),
                "phone_no": _contact.toString()
              }),
              headers: {"Content-Type": "application/json"});
          print("Status Code : " + response.statusCode.toString());

please try with below example, maybe it will help. Let me know if you need more support happy to help you.

Most probably when you return 404 status code, your API URL is not correct.

import 'dart:convert' as convert;

import 'package:http/http.dart' as http;

void main(List<String> arguments) async {
  var url =
      Uri.parse('https://reqbin.com/echo/post/json');

  // Await the http get response, then decode the json-formatted response.
  var response = await http.post(url, 
    headers:{
     'Accept': 'application/json'
    }, body: {"Customer": "Jason Sweet"}
  );
  print(response.statusCode);
  if (response.statusCode == 200) {
   print(response.body);
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM