Skip to content

Instantly share code, notes, and snippets.

@graphicbeacon
Last active November 21, 2018 11:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save graphicbeacon/7d2f241a39523731527e11f3bb675aae to your computer and use it in GitHub Desktop.
Save graphicbeacon/7d2f241a39523731527e11f3bb675aae to your computer and use it in GitHub Desktop.
Learn Dart #8: Perform a Server-side POST request in under 30 seconds (Solution)
import 'dart:io';
import 'dart:convert';
main() async {
var apiUrl = Uri.parse('https://jsonplaceholder.typicode.com/posts');
var client = HttpClient(); // `new` keyword optional
// 1. Create request
HttpClientRequest request = await client.postUrl(apiUrl);
// 2. Add payload to request
var payload = {
'title': 'Post 1',
'content': 'Lorem ipsum dolor sit amet',
};
request.write(json.encode(payload));
// 3. Send the request
HttpClientResponse response = await request.close();
// 4. Handle the response
var resStream = response.transform(Utf8Decoder());
await for (var data in resStream) {
print('Received data: $data');
}
}
@graphicbeacon
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment