从颤振发送 cURL 请求
Posted
技术标签:
【中文标题】从颤振发送 cURL 请求【英文标题】:send cURL request from flutter 【发布时间】:2022-01-06 05:01:50 【问题描述】:我需要向 api 发送 cURL 请求,但我没有正确理解文档。第一次使用 cURL。这是发送请求的详细信息。
# Steps to send request
# First get JSON Web Token
# Please get your Client Id and Client Secret from https://dashboard.groupdocs.cloud/applications.
# Kindly place Client Id in "client_id" and Client Secret in "client_secret" argument.
curl -v "https://api.groupdocs.cloud/connect/token" \
-X POST \
-d "grant_type#client_credentials&client_id#xxxx&client_secret#xxxx" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"
$ cURL example to join several documents into one
curl -v "https://api.groupdocs.cloud/v1.0/parser/text" \
-X POST \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-H "Authorization: Bearer
<jwt token>" \
-d "
"FileInfo":
"FilePath": "words\docx\document.docx",
"
这就是响应的方式
"text": "First Page\r\r\f"
【问题讨论】:
【参考方案1】:Curl 只是一个发送请求的工具 你可以在颤振中对 http 包做同样的事情 你的第一个 curl 请求相当于这个
var headers =
'Content-Type': 'application/x-www-form-urlencoded'
;
var request = http.Request('POST', Uri.parse('https://api.groupdocs.cloud/connect/token'));
request.bodyFields =
'grant_type': '',
'client_id': '',
'client_secret': ''
;
request.headers.addAll(headers);
http.StreamedResponse response = await request.send();
if (response.statusCode == 200)
print(await response.stream.bytesToString());
else
print(response.reasonPhrase);
第二次请求
var headers =
'Authorization': 'Bearer <jwt token>',
'Content-Type': 'application/json'
;
var request = http.Request('POST', Uri.parse('https://api.groupdocs.cloud/v1.0/parser/text'));
request.body = json.encode(
"FileInfo":
"FilePath": "words\\docx\\document.docx"
);
request.headers.addAll(headers);
http.StreamedResponse response = await request.send();
if (response.statusCode == 200)
print(await response.stream.bytesToString());
else
print(response.reasonPhrase);
了解http请求,使用postman之类的工具习惯它,然后使用http发送这些请求
【讨论】:
以上是关于从颤振发送 cURL 请求的主要内容,如果未能解决你的问题,请参考以下文章