Api Authorization working in postman but not working in flutter - flutter

I created an api with authorization, It is working correctly in postman but but i am not able to use authorization in flutter, I am getting Statuscode: 401(unauthorized) in flutter but in postman i am getting statuscode: 200(success). Here is my Code:
sharedPreferences = await SharedPreferences.getInstance();
String Authorization = sharedPreferences.getString("token");
var data = await http.get("http://10.148.7.58/Election/api/TblDatas",
headers: <String, String>{'Authorization': '$Authorization',}
);
//Also tried these
//headers: {'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': '$Authorization',}
//headers: {HttpHeaders.contentTypeHeader: "application/json", HttpHeaders.authorizationHeader: "$Authorization"}
print('status code is' + (data.statusCode).toString());

headers: <String, String>{
'Content-Type': 'application/json', //add context-type
'Accept': 'application/json',//add accept
'Authorization': 'Bearer $token',
}

Make sure that your server accepts lower case header names - in this case authorization, as Dart will lower case them. This fools some servers that aren't RFC compliant

Related

flutter list payment paypal

i am trying to get a paypal payment list in flutter, but i can't get it.
This is the documentation:
doc paypal api
but I don't know where I'm wrong.
This is my code:
var response = await http.post(
Uri.parse(
"https://api-m.sandbox.paypal.com/v1/payments/payment?count=10&start_index=0&sort_by=create_time&sort_order=desc"),
headers: {
"content-type": "application/json",
'Authorization': 'Bearer ' + accessToken
});
I'm getting a 400's error: INVALID_REQUEST
a help? thank you
sorry for the nonsense I realized now that it is of type get and not post.
Correct code:
var response = await http.get(
Uri.parse(
"https://api-m.sandbox.paypal.com/v1/payments/payment?count=10&start_index=0&sort_by=create_time&sort_order=desc"),
headers: {
"content-type": "application/json",
'Authorization': 'Bearer ' + accessToken
});

How to make a proper http request?

I can't seem to make a proper http request with my code. Whenever I run the following code,
var url = Uri.https('datamall2.mytransport.sg', '/ltaodataservice/BusArrivalv2?BusStopCode=', {'BusStopCode': busStopCode});
final response = await http.get(
url,
headers: <String, String>{
'Content-Type': 'application/json',
'AccountKey': accountKey,
},
);
I get the following error
I/flutter (24816): SocketException: OS Error: Connection refused, errno = 111, address = datamall2.mytransport.sg
This is the http request I'm trying to make
http://datamall2.mytransport.sg/ltaodataservice/BusArrivalv2?BusStopCode=busStopCode (busStopCode is a 5 digit number)
The wrong protocol is being used.
You've requested httpS.
The server is only responding to http.
You could try something simpler:
String url = 'http://datamall2.mytransport.sg/ltaodataservice/BusArrivalv2?$busStopCode';
final response = await http.get(
url,
headers: <String, String>{
'Content-Type': 'application/json',
'AccountKey': accountKey,
},
);
You should change your url variable to:
var url = Uri.http('datamall2.mytransport.sg', '/ltaodataservice/BusArrivalv2', {'BusStopCode': busStopCode});

Flutter HTTP calls using authorization fails but works in postman

I am using Flutter 1.20.4, http 0.12.2 package and I am having an issue where my HTTP calls are successful in Postman but fail in a flutter. I came across a number of articles talking about issue with lower case HTTP headers and some older servers. I don't have that issue as I have tested postman with lower case. I have checked my bearer token on jwt.io and issuer matches the domain I am using. Any call made from flutter that uses authorization will return as "not authenticated" so it would come up with HTTP 302 (redirect to login by identity provider). Any ideas?
My code looks like this:
import 'package:http/http.dart' as http;
...
var getProfileUrl = _identityApi + '/api/profile/get'; // TODO: CHANGE THIS
var accessToken = await _secureStorage.read(key: 'bearerToken');
var response = await http.get(getProfileUrl, headers: {
HttpHeaders.contentTypeHeader: 'application/json',
HttpHeaders.acceptHeader: 'application/json',
HttpHeaders.authorizationHeader: 'Bearer $accessToken'
});
POSTMAN:
FLUTTER:
try this
import 'package:http/http.dart' as http;
...
Map<String,String> _headers={
'content-type': 'application/json',
'accept: 'application/json',
'authorization': 'Bearer $accessToken'
};
var getProfileUrl = _identityApi + '/api/profile/get'; // TODO: CHANGE THIS
var accessToken = await _secureStorage.read(key: 'bearerToken');
var response = await http.get(getProfileUrl, headers: _headers);

How to send a token in a request in flutter?

I am making a flutter application, and i have written a server in django. When i send a token to my server for authentication then my server sends me an error of undefined token. Without token all requests works fine, but when i add a token then it gives me an error
{detail: Authentication credentials were not provided.}
But When i add token in modheader, my server works fine
Authorization: Token bff0e7675d6d80bd692f1be811da63e4182e4a5f
This is my flutter code
const url = 'MY_API_URL';
var authorization = 'Token bff0e7675d6d80bd692f1be811da63e4182e4a5f';
final response = await http.get(
url,
headers: {
'Content-Type': 'application/json',
'Authorization': authorization,
}
);
final responseData = json.decode(response.body);
print('responseData');
print(responseData);
try this:
Map<String, String> headers = {
HttpHeaders.contentTypeHeader: 'application/json',
HttpHeaders.acceptHeader: 'application/json',
HttpHeaders.authorizationHeader: 'Token bff0e7675d6d80bd692f1be811da63e4182e4a5f'
};
& use them in request
final response = await http.get(
url,
headers: headers,
);
As I don't know to work on your API so I can't tell you the exact answer.
Check that, Is your backend taking authorization by header or body or
I'll suggest you first make authorization by tools like postman then
if that succeeds then try to implement that in your app.

Dio options.contentType vs header "Content-Type"

I was trying to make a call to a REST service using the Dio plugin, but kept getting HTTP 400 response code. I thought I was doing everything right by setting the content type and response type options to JSON:
Response response = await Dio().get(
'https://api.example.com/v1/products/$productId',
queryParameters: {},
options: Options(
contentType: ContentType.json,
responseType: ResponseType.json,
headers: {'Authorization': 'Bearer $MY_API_KEY'}
),
);
However, it turns out that I needed to add a Content-Type header as well:
headers: {'Authorization': 'Bearer $MY_API_KEY'}, 'Content-Type': 'application/json' };
So now I'm confused - what exactly does the contentType option do? I thought it was analogous to setting the Content-Type header manually?
I've tried this locally using dio: ^3.0.10 and it seems that ContentType.json is an invalid value for contentType.
Digging through the documentation for dio, Headers.jsonContentType should be used.