Muhammad Amir
Muhammad Amir

Reputation: 672

flutter http.post is giving 500 error, while the api is working fine in postman

This login API is working fine in postman, but I am getting 500 status code error in flutter.

Below is the code for service where api being called:

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

class AuthService {
  static const API_URL =
      "http://ec2-15-185-73-129.me-south-1.compute.amazonaws.com";
  static const headers = {
    'Content-Type': 'application/json'
  };
  Future<APIResponse<AuthResponse>> signIn() async {
    var body = jsonEncode({"username": "username", "password": "password"});
    var response =
        await http.post(API_URL + "/api-token-auth", headers: headers, body: body)));
    print(response.body.toString());
    print(response.request.toString());
    if (response.statusCode == 200) {
      // not reaching here
    } else {
      print(handleError(response.statusCode));
      // response.statusCode is **500**

    }
  }
}

print(response.body.toString()) gives below output:

<!DOCTYPE html>
I/flutter ( 6158): <html lang="en">
I/flutter ( 6158): <head>
I/flutter ( 6158):   <meta http-equiv="content-type" content="text/html; charset=utf-8">
I/flutter ( 6158):   <meta name="robots" content="NONE,NOARCHIVE">
I/flutter ( 6158):   <title>RuntimeError
I/flutter ( 6158):           at /api-token-auth</title>
I/flutter ( 6158):   <style type="text/css">
I/flutter ( 6158):     html * { padding:0; margin:0; }
I/flutter ( 6158):     body * { padding:10px 20px; }
I/flutter ( 6158):     body * * { padding:0; }
I/flutter ( 6158):     body { font:small sans-serif; background-color:#fff; color:#000; }
I/flutter ( 6158):     body>div { border-bottom:1px solid #ddd; }
I/flutter ( 6158):     h1 { font-weight:normal; }
I/flutter ( 6158):     h2 { margin-bottom:.8em; }
I/flutter ( 6158):     h3 { margin:1em 0 .5em 0; }
I/flutter ( 6158):     h4 { margin:0 0 .5em 0; font-weight: normal; }
I/flutter ( 6158):     code, pre { font-size: 100%; white-space: pre-wrap; }
I/flutter ( 6158):     table { border:1px solid #ccc; border-collapse: collapse; width:100%; background:white; }
I/flutter ( 6158):     tbody td, tbody th { vertical-align:top; padding:2px 3px; }
I/flutter ( 6158):     thead th {
I/flutter ( 6158):       padding:1px 6px 1px 3px; background:#fefefe; text-align:left;
I/flutter ( 6158):       font-weight:normal; font-size:11px; border:1px solid #ddd;
I/flutter ( 6158):     }
I/flutter ( 6158):     tbody th { width:12em; text-align:right; colo

print(response.request.toString()) gives below output:

POST http://ec2-15-185-73-129.me-south-1.compute.amazonaws.com/api-token-auth

Please help Postman request output

Upvotes: 4

Views: 6990

Answers (4)

maykhid
maykhid

Reputation: 189

I got this error because I passed some wrong parameters in the body of my request. I would say check properly the body you're passing to your request.

Upvotes: 1

rufw91
rufw91

Reputation: 167

For anyone who has this issue it may be that the Host header is not being sent properly. I tested in postman and was quite surprised to find that this was the cause of my problem

Upvotes: 1

Raushan Jha
Raushan Jha

Reputation: 139

The 500 status code means that The server encountered an unexpected condition which prevented it from fulfilling the request. Please Check the url or add \ after the url like this:

 var response =
    await http.post(API_URL + "/api-token-auth\", headers: headers, body: body)));

Upvotes: 0

Ryosuke
Ryosuke

Reputation: 3892

Replace the URL:

API_URL + "/api-token-auth"

With

API_URL + "/api-token-auth/"

Its a little weird. Maybe something wrong with your server-side routing but with the trailing / its working.

Upvotes: 4

Related Questions