Flutter调用API无法获取响应:Postman正常但Flutter报‘Object Moved’异常求助
Hey there! Let's break down why you're hitting that "Object Moved" error in Flutter even though the API works perfectly in Postman.
First off, that error message is a dead giveaway: your server is sending a 3xx redirect response (like 301 or 302), but your current code doesn't handle these status codes at all. Postman automatically follows redirects by default, but Dart's http client doesn't always behave the same way—especially if there's a URL formatting issue or specific redirect rules the server enforces.
Here are the fixes you can try, step by step:
1. Fix URL formatting (a common culprit)
URL拼接错误经常触发 unintended redirects. Instead of string-concatenating your base URL and endpoint, use Uri.parse() or Uri.http()/Uri.https() to build a valid URL reliably:
// Replace your string concat with this final url = Uri.parse("$baseUrl/api/Course/List"); // Or even better, split base URL and path explicitly: // final url = Uri.https("your-server-domain.com", "api/Course/List");
This avoids issues like duplicate slashes (e.g., //api/Course/List) that can make the server redirect you to a corrected URL.
2. Explicitly enable redirect following
While Dart's http client has followRedirects: true by default, forcing it and setting a max redirect count can help in edge cases:
final response = await client.post( url, headers: {"content-type": "application/json"}, body: bodyMap, followRedirects: true, maxRedirects: 5, // Prevent infinite loops );
3. Add proper status code handling for redirects
Update your code to catch 3xx status codes and either let the client follow them or handle them manually:
var bodyMap = json.encode(map); final url = Uri.parse("$baseUrl/api/Course/List"); final response = await client.post( url, headers: {"content-type": "application/json"}, body: bodyMap, followRedirects: true, ); // First, log full details to debug print("##Status Code: ${response.statusCode}"); print("##Response Headers: ${response.headers}"); print("##Response Body: ${response.body}"); if (response.statusCode >= 200 && response.statusCode < 300) { // Handle success (200-299 range) print("##Got valid response"); return responseFromJson(response.body); } else if (response.statusCode == 400) { print("##Bad Request error"); return responseFromJson(response.body); } else if (response.statusCode >= 300 && response.statusCode < 400) { // If redirects aren't auto-following, grab the Location header and jump manually final redirectTarget = response.headers['location']; if (redirectTarget != null) { print("##Redirecting to: $redirectTarget"); final redirectedResponse = await client.get(Uri.parse(redirectTarget)); return responseFromJson(redirectedResponse.body); } else { throw Exception("Server sent redirect but no target URL provided"); } } else { throw Exception("Unexpected error: Status code ${response.statusCode}"); }
4. Match Postman's request exactly
Double-check that your Flutter request mirrors Postman's down to the smallest detail:
- Did Postman include extra headers (like
User-AgentorCookie) that your Flutter code is missing? Add them to theheadersmap if so. - Confirm the request body encoding is identical (Postman might be using form-data instead of JSON, but your
content-typeheader suggests you're on the right track here).
The core issue is that Postman hides the redirect process from you, while Flutter exposes the raw "Object Moved" response because your code wasn't set up to handle it. With these fixes, you should get the same successful response as Postman.
内容的提问来源于stack exchange,提问作者angelina

