1

First of all, I have been trying to get a HTTP request in Flutter, and i have no experience in JavaScript.

A Website only provides a JavaScript sample code but i don't how to use it for Flutter.

https://kodestat.gitbook.io/flutter/flutter-http-requests-and-rest-api

I tried the above link but failed.

I know i have to insert URL and ServiceKey like this.

class HomePageState extends State<HomePage> {

  //this async func will get data from the internet
  //when our func is done we return a string
  Future<String> getData() async {
    //we have to wait to get the data so we use 'await'
    http.Response response = await http.get(
      //Uri.encodeFull removes all the dashes or extra characters present in our Uri
      Uri.encodeFull("URL"),
      headers: {
        //if your api require key then pass your key here as well e.g "key": "my-long-key"
       "Accept": "application/json" 
       "key": "my-long-key"
      }
    );

    //print(response.body);

    List data = json.decode(response.body);
    //print(data);
    print(data[1]["title"]);
  }

But how should i handle encodeURIComponent and other functions in Flutter to get a HTTP request?

Here is JavaScript sample code.

var xhr = new XMLHttpRequest();
var url = 'http://openapi.tago.go.kr/openapi/service/ArvlInfoInqireService/getSttnAcctoArvlPrearngeInfoList'; /*URL*/
var queryParams = '?' + encodeURIComponent('ServiceKey') + '='+'ServiceKey'; /*Service Key*/
queryParams += '&' + encodeURIComponent('cityCode') + '=' + encodeURIComponent('25'); /*CityCode*/
queryParams += '&' + encodeURIComponent('nodeId') + '=' + encodeURIComponent('DJB8001793ND'); /*nodeID*/
xhr.open('GET', url + queryParams);
xhr.onreadystatechange = function () {
    if (this.readyState == 4) {
        alert('Status: '+this.status+' Headers: '+JSON.stringify(this.getAllResponseHeaders())+' Body: '+this.responseText);
    }
};

xhr.send('');

1 Answer 1

4

There is a dart package that provides some helper classes for http requests.

Github : https://github.com/Ephenodrom/Dart-Basic-Utils Install it with:

dependencies:
  basic_utils: ^1.3.0

Usage

Map<String, String> headers = {
  "Accept": "application/json",
  "key": "my-long-key"
};
Map<String, String> queryParameters = {
  "citycode": Uri.encodeFull("25"),
  "nodeId": Uri.encodeFull("123456789")
};

String URL = Uri.encodeFull("URL");
// If the api returns json
Map<String, dynamic> dataAsJson = await HttpUtils.getForJson(url,
      headers: headers, queryParameters: queryParameters);
// if the api returns plain strings
String dataAsString = await HttpUtils.getForString(url,
      headers: headers, queryParameters: queryParameters);
// if the api returns something else like XML, EPP, KV, YAML 
Response fullResponse = await HttpUtils.getForFullResponse(url,
      headers: headers, queryParameters: queryParameters);

Use Uri.encodeFull on your query parameter data.

Additional information :

These are all methods from the HttpUtils class.

Future<Map<Response> getForFullResponse(String url,{Map<String, dynamic> queryParameters,Map<String, String> headers});
Future<Map<String, dynamic>> getForJson(String url,{Map<String, dynamic> queryParameters,Map<String, String> headers});
Future<String> getForString(String url,{Map<String, dynamic> queryParameters,Map<String, String> headers});
Future<Map<Response> postForFullResponse(String url, String body,{Map<String, String> queryParameters,Map<String, String> headers});
Future<Map<String, dynamic>> postForJson(String url, String body,{Map<String, String> queryParameters,Map<String, String> headers});
Future<String> postForString(String url, String body,{Map<String, String> queryParameters,Map<String, String> headers});
Future<Response> putForFullResponse(String url, String body,{Map<String, String> queryParameters,Map<String, String> headers});
Future<Map<String, dynamic>> putForJson(String url, String body,{Map<String, String> queryParameters,Map<String, String> headers});
Future<String> putForString(String url, String body,{Map<String, String> queryParameters,Map<String, String> headers});
Future<Response deleteForFullResponse(String url,{Map<String, String> queryParameters,Map<String, String> headers});
Future<Map<String, dynamic>> deleteForJson(String url,{Map<String, String> queryParameters,Map<String, String> headers});
Future<String> deleteForString(String url,{Map<String, String> queryParameters,Map<String, String> headers});
Map<String, dynamic> getQueryParameterFromUrl(String url);
String addQueryParameterToUrl(String url, Map<String, dynamic> queryParameters);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.