Reputation: 7128
I have following curl which works on postman
curl --location --request POST 'https://example.com/api/user' \
--header 'email: [email protected]' \
--form 'amount="5000"' \
--form 'payment_type_id="7"' \
--form 'agent_id="1334"' \
--form 'phone="0123456789"'
But when I try to make it work in flutter http request it does not get email
value in headers.
Future getSells() async {
var response = await http.post(
Uri.parse('https://example.com/api/user'),
headers: {
HttpHeaders.acceptHeader: 'application/json',
"email": '[email protected]',
},
body: {
'amount': '5000',
'payment_type_id': '7',
'agent_id': '1334',
'phone': '0123456789',
},
);
if (response.statusCode == 200) {
var jsonResponse = convert.jsonDecode(response.body) as Map<String, dynamic>;
if (kDebugMode) {
print('data: $jsonResponse'); // result below
}
return jsonResponse['data'];
} else {
if (kDebugMode) {
print('Err1::: ${response.statusCode}');
print('Err2::: ${response.body}');
}
return [];
}
}
Error
I/flutter ( 6352): data: {error: access-denied}
Any suggestion of how to pass email
in headers?
Upvotes: 0
Views: 832
Reputation: 2425
You can do this way as its form request :
var uri = Uri.parse('https://example.com/api/user');
var request = MultipartRequest('POST', uri);
request.fields['amount'] = '5000';
request.fields['payment_type_id'] = '7';
request.fields['agent_id'] = '1334';
request.fields['phone'] = '0123456789';
request.headers.addAll( {'email' : '[email protected]' });
try {
var streamedResponse = await request.send();
var response = await Response.fromStream(streamedResponse);
print(response.body);
var data = jsonDecode(response.body);
var keyData= data['data'];
print(keyData);
} catch (e) {
rethrow;
}
Upvotes: 1