HTTP request in flutter

后端 未结 4 1516
-上瘾入骨i
-上瘾入骨i 2020-12-23 14:58

I am using Retrofit for Android. It is easy to retrieve and upload JSON via a REST based web service. Can we get any library which is equivalent to Retrofit for web services

相关标签:
4条回答
  • 2020-12-23 15:42

    get to the location of pubspac.yaml run cmd

    > flutter pub get 
    > flutter packages get
    

    add

    http: ^0.11.3+16

    in pubspac.yaml

    get dependencies. try to import library, hopes this helps!

    0 讨论(0)
  • 2020-12-23 15:49

    How to make HTTP requests in Flutter

    This answer tells how to make HTTP requests using the http package by the Dart team. If more advanced functionality is needed, check out the Dio package mentioned in the comments.

    We will be using JSONPlaceholder as a target for our API examples below.

    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
    

    Setup

    Add the http package dependency in pubspec.yaml.

    dependencies:
      http: ^0.12.0+1
    

    GET requests

    _makeGetRequest() async {
    
      // make request
      Response response = await get('https://jsonplaceholder.typicode.com/posts');
    
      // sample info available in response
      int statusCode = response.statusCode;
      Map<String, String> headers = response.headers;
      String contentType = headers['content-type'];
      String json = response.body;
    
      // TODO convert json to object...
    
    }
    

    Replace /posts with /posts/1 and the other GET requests mentioned above. Using posts returns an array of JSON objects while /posts/1 returns a single JSON object. You can use dart:convert to convert the raw JSON string to objects.

    POST request

    _makePostRequest() async {
    
      // set up POST request arguments
      String url = 'https://jsonplaceholder.typicode.com/posts';
      Map<String, String> 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 request

    A PUT request is meant to replace a resource or create it if it doesn't exist.

    _makePutRequest() async {
    
      // set up PUT request arguments
      String url = 'https://jsonplaceholder.typicode.com/posts/1';
      Map<String, String> 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 request

    A PATCH request is meant to modify a existing resource.

    _makePatchRequest() async {
    
      // set up PATCH request arguments
      String url = 'https://jsonplaceholder.typicode.com/posts/1';
      Map<String, String> 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\nsuscipit recusandae... (old body text not changed)",
      // }
    
    }
    

    Notice that the JSON string that is passed in only includes the title, not the other parts like in the PUT example.

    DELETE request

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

    Authentication

    Although the demo site we used above did not require it, if you need to include authentication headers, you can do it like this:

    Basic Authentication

    // import 'dart:convert'
    
    final username = 'username';
    final password = 'password';
    final credentials = '$username:$password';
    final stringToBase64Url = utf8.fuse(base64Url);
    final encodedCredentials = stringToBase64Url.encode(credentials);
    
    Map<String, String> headers = {
      HttpHeaders.contentTypeHeader: "application/json", // or whatever
      HttpHeaders.authorizationHeader: "Basic $encodedCredentials",
    };
    

    Bearer (token) Authentication

    // import 'dart:io';
    
    final token = 'WIiOiIxMjM0NTY3ODkwIiwibmFtZSI6Ikpv';
    
    Map<String, String> headers = {
      HttpHeaders.contentTypeHeader: "application/json", // or whatever
      HttpHeaders.authorizationHeader: "Bearer $token",
    };
    

    Related

    • What is the difference between PUT, POST and PATCH?
    0 讨论(0)
  • 2020-12-23 15:50

    Well for me at least you will need 3 packages.

    1. json_serializable
    2. Dio Http Client
    3. retrofit for dart

    I have tried chopper but I prefer the dio way of doing things. it is closer and more flexible.

    0 讨论(0)
  • 2020-12-23 15:57

    Just simple use this plug and little code for GET http url

    add this plugin from here httppackage

     http: ^0.12.0+2
    

    USAGE

    final uri = Constants.BASE_URL + 'endpoint';
        final headers = {'Content-Type': 'application/x-www-form-urlencoded'};//if required
        Response getResponse = await get(uri, headers: headers);
        int statusCode = getResponse.statusCode;
        String responseBody = getResponse.body;
        print('response----' + responseBody);
    
    0 讨论(0)
提交回复
热议问题