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

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -