gpt4 book ai didi

dart - 在 Aqueduct Dart 中发送帖子请求

转载 作者:行者123 更新时间:2023-12-03 03:58:00 25 4
gpt4 key购买 nike

我在 aqueduct dart 中创建了一个发布请求,它以 json 作为正文参数,我需要将该请求正文发送到第三方 api ,在从第三方 api 获得响应后,我需要将该响应返回给用户。我已经更新了代码并打印了响应 header ,上面写着 http 400(错误请求)

这是代码:

@override
Controller get entryPoint {
String dataRecieved;
var completer = new Completer();
var contents = new StringBuffer();
final router = Router();
// Prefer to use `link` instead of `linkFunction`.
// See: https://aqueduct.io/docs/http/request_controller/
router.route("/uploadurl").linkFunction((request) async {
final req = await request.body.decode();

// print( await request.body.decode());
HttpClient client = new HttpClient();
client.badCertificateCallback =
((X509Certificate cert, String host, int port) => true);
var auth = 'Bearer ' +
'eyJ...';

await client
.postUrl(Uri.parse(
'https://<removed>/api/datalake/v3/generateDownloadObjectUrls'))
.then((HttpClientRequest requestSend) {
requestSend.headers
.add("Content-Type", "application/json; charset=UTF-8");
requestSend.headers.add("Authorization", auth);
// requestSend.headers.contentLength = request.body.length;
print(req);
requestSend.write(req);
return requestSend.close();
}).then((HttpClientResponse response) async {
print(await response.contentLength);
var resStream = response.transform(Utf8Decoder());
await for (var data in resStream) {
print('Received data: $data');
}
print(await response.statusCode);


}).catchError((e) {
print("Request error: $e"); // The only case
});

print(contents);
return Response.ok({"key": dataRecieved});
});



return router;

}

当我向 postman 提出要求时,我得到
{
“关键”:空
}

我想我无法向第三方 API 发送正确的请求,因为当我从 postman 测试第三方 API 时,它发送了正确的响应

我的 pubspec.yaml 文件是:
name: proxydl
description: An empty Aqueduct application.
version: 0.0.1
author: stable|kernel <jobs@stablekernel.com>

environment:
sdk: ">=2.0.0 <3.0.0"

dependencies:
aqueduct: ^3.0.0
http: ^0.12.0+2

dev_dependencies:
test: ^1.0.0
aqueduct_test: ^1.0.0

这是我从 postman 发送的帖子请求:
{
"paths": [
{
"path": "/f1/f2.log"
}
]
}

这是我第一个在服务器端使用 Dart 的 POC。

最佳答案

经过进一步调查,我找到了答案:

最终请求 = 等待 request.body.decode();
var envalue = json.encode(req);

目前,这行得通,但我觉得可能有更好的答案

关于dart - 在 Aqueduct Dart 中发送帖子请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59572996/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com