在获取请求中发送标题

时间:2020-08-11 05:01:58

标签: flutter dart

我想在Flutter中发送获取请求请求。但是我一直在找回HTML代码。

在邮递员中,我必须指定accept: application/json才能获取JSON。但是我关注的教程没有提到。

这是我的下面的代码

  void getData() async {
    http.Response response = await http.get(
        'https://libriboo.com/api/landing');
  //  var res = json.decode(response.body);
    print(response.body);
  }

我得到了

I/flutter ( 5134):   <head>
I/flutter ( 5134):     <title>Libri</title>
I/flutter ( 5134):     <!-- Required meta tags -->
I/flutter ( 5134):     <meta charset="utf-8">
I/flutter ( 5134):     <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
I/flutter ( 5134):     <link rel="shortcut icon" type="image/png" href=".\images\favicon.png">
I/flutter ( 5134):     <!-- Bootstrap CSS -->
I/flutter ( 5134):     <link rel="stylesheet" href=".\assets\css\style.css">
I/flutter ( 5134):     <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
I/flutter ( 5134):     <link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
I/flutter ( 5134):     <link rel="stylesheet" href=".\assets\css\bootstrap.css">
I/flutter ( 5134):     <link rel="stylesheet" href=".\assets\css\font-awesome.css">
I/flutter ( 5134):     <script src="..\assets\js\script.js"></script>
I/flutter ( 5134): 
I/flutter ( 5134): 
I/flutter ( 5134): 
I/flutter ( 5134):   </head

2 个答案:

答案 0 :(得分:1)

xtremeCODE,您可以在content type中指定header,如下所示:

void getData() async {
    http.Response response = await http.get(
        'https://libriboo.com/api/landing',headers:{
        "Content-Type":"application/json"
    });
    print(response.body);
  }

答案 1 :(得分:0)

似乎在请求过程中出现服务器端错误,请尝试打印statusCode或尝试类似的操作

#include <utility>

// lambda_t is the type of thing we want to call.
// capture_t is the type of a helper object that 
// contains all all parameters meant to be passed to the callable
template< class lambda_t, class capture_t >
struct CallObject {

    lambda_t  m_lambda;
    capture_t m_args;

    typedef decltype( m_args(m_lambda) ) return_t;

    //Construct the CallObject by perfect forwarding which is
    //neccessary as they may these are lambda which will have
    //captured objects and we dont want uneccessary copies
    //while passing these around
    CallObject( lambda_t&& p_lambda, capture_t&& p_args ) :
        m_lambda{ std::forward<lambda_t>(p_lambda) },
        m_args  { std::forward<capture_t>(p_args) }
    {

    }

    //Applies the arguments captured in m_args to the thing
    //we actually want to call
    return_t invoke() {
        return m_args(m_lambda);
    }

    //Deleting special members for testing purposes
    CallObject() = delete;
    CallObject( const CallObject& ) = delete;
    CallObject( CallObject&& ) = delete;
    CallObject& operator=( const CallObject& ) = delete;
    CallObject& operator=( CallObject&& ) = delete;
};

//Factory helper function that is needed to create a helper
//object that contains all the paremeters required for the 
//callable. Aswell as for helping to properly templatize
//the CallObject
template< class lambda_t, class ... Tn >
auto Factory( lambda_t&& p_lambda, Tn&& ... p_argn ){

    //Using a lambda as helper object to contain all the required paramters for the callable
    //This conviently allows for storing value, references and so on
    auto x = [&p_argn...]( lambda_t& pp_lambda ) mutable -> decltype(auto) {

        return pp_lambda( std::forward<decltype(p_argn)>(p_argn) ... );
    };

    typedef decltype(x) xt;
    //explicit templetization is not needed in this case but
    //for the sake of readability it needed here since we then
    //need to forward the lambda that captures the arguments
    return CallObject< lambda_t, xt >( std::forward<lambda_t>(p_lambda), std::forward<xt>(x) );
}

int main(){

    auto xx = Factory( []( int a, int b ){

        return a+b;

    }, 10, 3 );

    int q = xx.invoke();

    return q;
}