Home > front end >  send cURL request from flutter
send cURL request from flutter

Time:12-20

I need to send cURL request to an api but i don't understand the documentation properly. First time working with cURL. Here is the details written to send request.

# 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",
    }
}"

This is how the response will come

{
    "text": "First Page\r\r\f"
}

CodePudding user response:

Curl is just a tool for sending requests you can do the same with http package in flutter your first request with curl is equivalent to this

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);
}

second request

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);
}

learn about http request , use a tool like postman to get used to it and then use http to send those requests

  • Related