我正在尝试使用 flutter 插件 HTTP 发出 HTTP POST 请求,但出现标题错误。 有没有人知道这是什么原因,因为在我的其他应用程序中这工作得很好?
await http.post(Uri.encodeFull("https://api.instagram.com/oauth/access_token"), body: {
"client_id": clientID,
"redirect_uri": redirectUri,
"client_secret": appSecret,
"code": authorizationCode,
"grant_type": "authorization_code"
});
答案 0 :(得分:82)
为了提高编译时类型安全性,package:http
0.13.0 introduced breaking changes 使之前接受 Uri
或 String
的所有函数现在只接受 {{1} } 代替。您将需要明确使用 Uri.parse
从 Uri
创建 Uri
。 (String
以前在内部为您调用过。)
旧代码 | 替换为 |
---|---|
package:http |
http.get(someString) |
http.get(Uri.parse(someString)) |
http.post(someString) |
(等等。)
在您的具体示例中,您需要使用:
http.post(Uri.parse(someString))
答案 1 :(得分:1)
如果你不想使用 parse 而想用简单的方法,这里是例子:
假设您想从以下位置获取一些数据:- https://example.com/helloworld
它对我有用:
String url ='example.com';
int helloworld=5;
http.client;
client.get(Uri.https(url,'/$helloworld'),
headers:{Content-type':'application/json',
},
);
如果你想得到:
http://example.com/helloworld
代替
https://example.com/helloworld
您只需更改 Uri.http()
,仅此而已。
这是正确的。 http.get
或 http.post
现在接受 only Uri
s ,但正如您所见,这没什么大不了的。