CURL 代码运行良好,可以使用 sendGrid 发送电子邮件, 我想为Flutter Dart应用程序转换此代码(因为sendGrid没有任何API(
curl --request POST --url https://api.sendgrid.com/v3/mail/send --header 'Authorization: Bearer
API_KEY' --header 'Content-Type: application/json'
--data '{"personalizations": [{"to": [{"email": "test@gmail.com"}]}],
"from": {"email": "test@hotmail.com"},"subject": "Hello, World!",
"content": [{"type": "text/plain", "value": "Hello World App"}]}'
请提供完整的功能
我尝试过这样的事情...但它不起作用
Future<Map<String, dynamic>> postRequest() async {
var url = 'https://api.sendgrid.com/v3/mail/send';
var body = json.encode({
"personalizations": [
{
"to": [
{"email": "test@gmail.com"}
]
}
],
"from": {"email": "test@hotmail.com"},
"subject": "Hello, World!",
"content": [
{"type": "text/plain", "value": "sent from flutter "}
]
});
var response = await http.post(
url,
headers: {
'Authorization':
'Bearer API_KEY',
'Content-Type': 'application/json',
},
body: body,
);
// todo - handle non-200 status code, etc
print(json.decode(response.body))
return json.decode(response.body);
}
你能试试这个吗?
import 'package:http/http.dart' as http;
void main() async {
var headers = {
'Authorization': 'Bearer API_KEY',
'Content-Type': 'application/json',
};
var data = '{"personalizations": [{"to": [{"email": "test@gmail.com"}]}], "from": {"email": "test@hotmail.com"},"subject": "Hello, World!", "content": [{"type": "text/plain", "value": "Hello World App"}]}';
var response = await http.post('https://api.sendgrid.com/v3/mail/send', headers: headers, body: data);
print(response.body);
if (response.statusCode != 200) throw Exception('http.post error: statusCode= ${response.statusCode}');
}