2014-06-12 21 views
3

正確的,所以我一直在工作的東西,需要通過頭,基本認證,並通過HTTP POST傳遞一些變量。這是一個終端應用程序。做一個HTTP POST與標題和身體

這是我的代碼如下所示:

import 'package:http/http.dart' as http; 
import 'dart:io'; 
void main() { 
    var url = "http://httpbin.org/post"; 
    var client = new http.Client(); 
    var request = new http.Request('POST', Uri.parse(url)); 
    var body = {'content':'this is a test', 'email':'[email protected]', 'number':'441276300056'}; 
    request.headers[HttpHeaders.CONTENT_TYPE] = 'application/json; charset=utf-8'; 
    request.headers[HttpHeaders.AUTHORIZATION] = 'Basic 021215421fbe4b0d27f:e74b71bbce'; 
    request.body = body; 
    var future = client.send(request).then((response) => response.stream.bytesToString().then((value) => print(value.toString()))).catchError((error) => print(error.toString())); 
} 

我使用httpbin作爲回聲服務器,因此它告訴我,我傳遞我的代碼工作正常,如果我不」通過身體,或者如果我傳遞一個字符串作爲身體。

很明顯,這是因爲http.Request中的body屬性只接受字符串,而我試圖將地圖傳遞給它。

我可以將其轉換爲字符串,它可能會工作,但我仍然認爲我的代碼可以改進。不是從語法的角度來看,也不是從它如何處理未來,但我不確定使用http.dart是正確的做法。

難道有人指着我正確的方向嗎?

在此先感謝。

回答

5

JSON 一個字符串。您需要將地圖編碼爲JSON並將其作爲字符串傳遞。

您可以使用bodyFields而不是body來傳遞地圖。
這樣你的content-type固定爲"application/x-www-form-urlencoded"

post的DartDoc說:

/// If [body] is a Map, it's encoded as form fields using [encoding]. The 
/// content-type of the request will be set to 
/// `"application/x-www-form-urlencoded"`; this cannot be overridden. 

我能夠這樣發送JSON數據前一陣子

return new http.Client() 
    .post(url, headers: {'Content-type': 'application/json'}, 
     body: JSON.encoder.convert({"distinct": "users","key": "account","query": {"active":true}})) 
     .then((http.Response r) => r.body) 
     .whenComplete(() => print('completed')); 

編輯

import 'package:http/http.dart' as http; 
import 'dart:io'; 
void main() { 
    var url = "http://httpbin.org/post"; 
    var client = new http.Client(); 
    var request = new http.Request('POST', Uri.parse(url)); 
    var body = {'content':'this is a test', 'email':'[email protected]', 'number':'441276300056'}; 
// request.headers[HttpHeaders.CONTENT_TYPE] = 'application/json; charset=utf-8'; 
    request.headers[HttpHeaders.AUTHORIZATION] = 'Basic 021215421fbe4b0d27f:e74b71bbce'; 
    request.bodyFields = body; 
    var future = client.send(request).then((response) 
     => response.stream.bytesToString().then((value) 
      => print(value.toString()))).catchError((error) => print(error.toString())); 
} 

生產

{ 
    "args": {}, 
    "data": "", 
    "files": {}, 
    "form": { 
    "content": "this is a test", 
    "email": "[email protected]", 
    "number": "441276300056" 
    }, 
    "headers": { 
    "Accept-Encoding": "gzip", 
    "Authorization": "Basic 021215421fbe4b0d27f:e74b71bbce", 
    "Connection": "close", 
    "Content-Length": "63", 
    "Content-Type": "application/x-www-form-urlencoded; charset=utf-8", 
    "Host": "httpbin.org", 
    "User-Agent": "Dart/1.5 (dart:io)", 
    "X-Request-Id": "b108713b-d746-49de-b9c2-61823a93f629" 
    }, 
    "json": null, 
    "origin": "91.118.62.43", 
    "url": "http://httpbin.org/post" 
} 
+0

謝謝!雖然它看起來並沒有將值作爲一種形式傳遞(例如,如果您使用我提到的回顯服務器提出請求,您會看到「form」:{}「is impty –

+0

當您在地圖中使用'bodyFields'或當你用'JSON.encoder.convert'和內容類型''application/json''使用'body'? –

+0

如果你使用url http://httpbin.org/post運行你的例子並且執行「.then(( http.Response r)=> print(r.body))「你會看到它 –