How to do POST in Dart command line HttpClient -
i'm struggling putting dart command line client capable of doing http post. know can not use dart:html library , have use dart:io
the beginning seems simple:
httpclient client = new httpclient(); client.geturl(uri.parse("http://my.host.com:8080/article"));
the question is: correct syntax , sequence make httpclient
post , able pass json-encoded string post?
use http package , dart:convert
import 'package:http/http.dart' http; import 'dart:convert'; void main() { var url = 'http://httpbin.org/post'; http.post(url, body: json.encode({'test': 'value'})).then((response) { print("response status: ${response.statuscode}"); print("response body: ${response.body}"); }); }
for adding custom headers, handling errors etc. see https://www.dartlang.org/dart-by-example/#making-a-post-request
Comments
Post a Comment