【问题标题】:flutter remote access to woocommerce with getx flutter使用 getx flutter 远程访问 woocommerce
【发布时间】:2021-07-09 20:52:07
【问题描述】:

我正在尝试通过 api 连接到我的 woocommerce,但它显示错误 401

class RemoteServices{
static var client = http.Client();
static Future<List<Welcome>> fetchProducts() async{
var response =await client.get("https://mywebsite/wp-json/wc/v3/products",headers: <String,String>{'Usermname':('ck_key'),'Password':'cs_key' });
  if(response.statusCode==200){
    var jsontString = response.body;
    return welcomeFromJson(jsontString);
    
  }
  else{
    print("error api");
    print(response.statusCode);
  }
}
}

它总是显示错误 401

【问题讨论】:

标签: api flutter dart


【解决方案1】:

您的网址没有按照您尝试的方式正确构建。

这是使用GetConnect 执行此操作的一种方法的示例

final map = {'apikey': 'yourApiKey'}; //equivalent to adding apiKey=yourApiKey to the url
final response = await client.get("https://mywebsite/wp-json/wc/v3/products", headers: map);

您也不必在 get 调用中使用 headers 属性。如果您不熟悉调用 APIs,那么更简单的开始方法是将完整的完整 url 传递给 get 方法,这样也可以正常工作。

无论哪种方式,您都需要在浏览器中确认正确的 url,然后确保将其传递给 get 函数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-08-04
    • 2021-05-10
    • 1970-01-01
    • 2022-12-01
    • 2021-06-18
    • 2021-07-14
    • 1970-01-01
    • 2021-10-10
    相关资源
    最近更新 更多