http_requests 2.5.0 http_requests: ^2.5.0 copied to clipboard
HTTP Requests Package Inspired By Python Requests Module Which Is Used For To Make Http Request And Get Response. You Can Use It In Rest Api
HTTP Requests #
HTTP Requests Package Inspired By Python Requests Module
Which Is Used For To Make HTTP Request And Get Response. You Can Use It In Rest API
Install #
Add this to your package's pubspec.yaml file:
dependencies:
http_requests: ^2.5.0
Usage #
Start by importing the library
import 'package:http_requests/http_requests.dart';
Let's make a simple example of GET Method
Response response = await HttpRequests.get('https://www.secanonm.com');
print(response.statusCode);
Let's make a simple example of POST Method
Map<String,String> data = {'key' : 'value'};
Response response = await HttpRequests.post('https://www.secanonm.in',data:data);
print(response.status);
Let's make a simple example of Execption Handling
Map<String,String> data = {'key' : 'value'};
try {
Response response = await HttpRequests.post('https://secanonm.in',data:data);
print(response.json);
} on HttpRequestExecption catch (e) {
print(e.code);
}
print(r.status);
Some Methods #
just like in python's request module, the Response
object has this functionallity
r.statusCode
- the response status coder.status
- the response status code with description of code like200 - success
r.url
- the url in the requestr.headers
- the response headersr.success
- a boolean.true
indicates that the request was a great successr.hasError
- a boolean.true
indicates that the request was not a great successr.bytes
- return the body in the respone as a list of bytesr.contentLength
- return the response content lenghtr.contentType
- return the response content typeapplication/json
,r.isRedirect
- return the Redirection Status istrue
orfalse
r.content
- return the body in the respone as a string (with UTF-8)r.response
- return the body in the respone as a string (without UTF-8 {take default})r.json
- recodes the body in the respone and returns the result (dynamic type)r.throwForStatus()
- will throw an exception if the responsestatusCode
is not a great success.
HTTP REQUESTS Execption #
invalid url
- it's occurs when you provide invalid urltimeout
- it's occurs when request timeoutdefault 10 seconds
unreachable
- it's occurs when host is unreachable due to slow or no internet