android http patch请求,android – flutter中的HTTP请求

如何在Flutter中发出HTTP请求

这个答案讲述了如何使用Dart团队使用http package发出HTTP请求.如果需要更高级的功能,请查看注释中提到的Dio package.

我们将使用JSONPlaceholder作为以下API示例的目标.

GET /posts

GET /posts/1

GET /posts/1/comments

GET /comments?postId=1

GET /posts?userId=1

POST /posts

PUT /posts/1

PATCH /posts/1

DELETE /posts/1

设定

在pubspec.yaml中添加http包依赖项.

dependencies:

http: ^0.12.0+1

GET请求

_makeGetRequest() async {

// make request

Response response = await get('https://jsonplaceholder.typicode.com/posts');

// sample info available in response

int statusCode = response.statusCode;

Map headers = response.headers;

String contentType = headers['content-type'];

String json = response.body;

// TODO convert json to object...

}

替换/发布/ posts / 1以及上面提到的其他GET请求.使用posts返回一个JSON对象数组,而/ posts / 1返回一个JSON对象.您可以使用dart:convert将原始JSON字符串转换为对象.

POST请求

_makePostRequest() async {

// set up POST request arguments

String url = 'https://jsonplaceholder.typicode.com/posts';

Map headers = {"Content-type": "application/json"};

String json = '{"title": "Hello", "body": "body text", "userId": 1}';

// make POST request

Response response = await post(url, headers: headers, body: json);

// check the status code for the result

int statusCode = response.statusCode;

// this API passes back the id of the new item added to the body

String body = response.body;

// {

// "title": "Hello",

// "body": "body text",

// "userId": 1,

// "id": 101

// }

}

PUT请求

PUT请求旨在替换资源或创建它(如果它不存在).

_makePutRequest() async {

// set up PUT request arguments

String url = 'https://jsonplaceholder.typicode.com/posts/1';

Map headers = {"Content-type": "application/json"};

String json = '{"title": "Hello", "body": "body text", "userId": 1}';

// make PUT request

Response response = await put(url, headers: headers, body: json);

// check the status code for the result

int statusCode = response.statusCode;

// this API passes back the updated item with the id added

String body = response.body;

// {

// "title": "Hello",

// "body": "body text",

// "userId": 1,

// "id": 1

// }

}

PATCH请求

PATCH请求旨在修改现有资源.

_makePatchRequest() async {

// set up PATCH request arguments

String url = 'https://jsonplaceholder.typicode.com/posts/1';

Map headers = {"Content-type": "application/json"};

String json = '{"title": "Hello"}';

// make PATCH request

Response response = await patch(url, headers: headers, body: json);

// check the status code for the result

int statusCode = response.statusCode;

// only the title is updated

String body = response.body;

// {

// "userId": 1,

// "id": 1

// "title": "Hello",

// "body": "quia et suscipit

suscipit recusandae... (old body text not changed)",

// }

}

请注意,传入的JSON字符串仅包含标题,而不包括PUT示例中的其他部分.

删除请求

_makeDeleteRequest() async {

// post 1

String url = 'https://jsonplaceholder.typicode.com/posts/1';

// make DELETE request

Response response = await delete(url);

// check the status code for the result

int statusCode = response.statusCode;

}

认证

虽然我们上面使用的演示站点不需要它,但如果您需要包含身份验证标头,您可以这样做:

基本认证

// import 'dart:convert'

final username = 'username';

final password = 'password';

final credentials = '$username:$password';

final stringToBase64Url = utf8.fuse(base64Url);

final encodedCredentials = stringToBase64Url.encode(credentials);

Map headers = {

HttpHeaders.contentTypeHeader: "application/json", // or whatever

HttpHeaders.authorizationHeader: "Basic $encodedCredentials",

};

承载(令牌)认证

// import 'dart:io';

final token = 'WIiOiIxMjM0NTY3ODkwIiwibmFtZSI6Ikpv';

Map headers = {

HttpHeaders.contentTypeHeader: "application/json", // or whatever

HttpHeaders.authorizationHeader: "Bearer $token",

};

有关

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值