- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用颤振应用程序与 azure cosmos db 交谈。 cosmos db 有一个基于 sql 查询的 rest api。其中内容类型应为 application/query+json。
我尝试使用 postman 连接到 cosmos db 它工作但在颤动中我遇到了一个异常
Bad state: Cannot set the body fields of a Request with content-type "application/query+json"
final http.Response response =
await http.post(
url,
headers: {<other headers>, 'Content-Type' : 'application/query+json'},
body: {
"query" : "<SQL Query>",
"parameters" : [<parameter name and value pairs>]
},
);
最佳答案
尝试使用 HttpClient:
Future<HttpClientResponse> httpRequest(String url, Map headers, Map body, {bool isQuery = false}) async {
HttpClient httpClient = new HttpClient();
HttpClientRequest request = await httpClient.postUrl(Uri.parse(url));
headers.forEach((key, value) {
request.headers.set(key,value);
});
if(isQuery) {
request.headers.set('content-type', 'application/query+json');
request.headers.set('x-ms-documentdb-isquery', 'True');
}
if(body != null) {
request.add(utf8.encode(json.encode(body)));
}
HttpClientResponse response = await request.close();
httpClient.close();
return response;
}
Future<HttpClientResponse> doSELECTQuery(String userAgent, List<String> requiredColHeaders, String azTblName,
{Map<String,String> mapWHERE, String pKey, String pKey_Filter, Map<String, String> moreHeaders, String alternateCollectionUrl = null}) async {
String _collectionUrl;
String _host;
Uri uri = Uri.tryParse(alternateCollectionUrl);
if(uri != null) {
_host = uri.host;
}
_collectionUrl = alternateCollectionUrl;
} else {
_collectionUrl = this.collectionUrl;
_host = this.host;
}
String collectionPath = Uri.parse(_collectionUrl).path.substring(1); // remove leading "/"
Map<String, String> headers = {
"authorization": generateAuthToken(
"POST", "docs", collectionPath, HttpDate.format(DateTime.now()),
masterKey, "master"),
"x-ms-version": "2018-12-31",
"x-ms-date": HttpDate.format(DateTime.now()),
"user-agent": userAgent,
"Cache-Control": "no-cache",
"Accept": "application/json",
"Host": _host,
"x-ms-documentdb-partitionkey": pKey == null ? "" : "[\"" + pKey_Filter + "\"]",
"x-ms-documentdb-query-enablecrosspartition": pKey == null ? "True" : ""
};
if(moreHeaders != null) {
headers.addAll(moreHeaders);
}
String strSELECT = "";
requiredColHeaders.forEach((col) {
strSELECT = strSELECT + "$azTblName[\"$col\"],";
});
strSELECT = strSELECT.substring(0, strSELECT.length - 1); // remove trailing ","
String strWHERE = "";
if(mapWHERE != null) {
mapWHERE.forEach((col, value) {
strWHERE = strWHERE + "($azTblName[\"$col\"] = \"$value\")AND";
});
strWHERE = strWHERE.substring(0, strWHERE.length - 3); // remove trailing "AND"
}
String strQuery = "SELECT $strSELECT FROM $azTblName";
if(strWHERE != "") {
strQuery = strQuery + " WHERE $strWHERE";
}
Map<String, dynamic> body = {
"query": strQuery,
"parameters": []
};
var response = await httpRequest(_collectionUrl + "/docs", headers, body, isQuery: true);
return response;
}
import 'dart:convert';
import 'dart:io';
class azureCosmosHelper {
String masterKey = "<your_master_key>";
// Contains url to collection
String collectionUrl = "https://<your_database_account>.documents.azure.com/dbs/<your_database_id>/colls/<your_collection_id>";
String host = "<your_database_account>.documents.azure.com";
String pKey = "<your_partition_key>";
Future<void> getAzureDB() {
HttpClientResponse response = await doSELECTQuery("<userAgent>", List<String> tableColumns, "<tableName>",
pKey: this.pKey,
pKey_Filter: "<pKey_Filter>",
);
// parse response accordingly
}
Future<HttpClientResponse> doSELECTQuery(String userAgent, List<String> requiredColHeaders, String azTblName,
{Map<String,String> mapWHERE, String pKey, String pKey_Filter, Map<String, String> moreHeaders, String alternateCollectionUrl = null}) async {
String _collectionUrl;
String _host;
Uri uri = Uri.tryParse(alternateCollectionUrl);
if(uri != null) {
_host = uri.host;
}
_collectionUrl = alternateCollectionUrl;
} else {
_collectionUrl = this.collectionUrl;
_host = this.host;
}
String collectionPath = Uri.parse(_collectionUrl).path.substring(1); // remove leading "/"
Map<String, String> headers = {
"authorization": generateAuthToken(
"POST", "docs", collectionPath, HttpDate.format(DateTime.now()),
masterKey, "master"),
"x-ms-version": "2018-12-31",
"x-ms-date": HttpDate.format(DateTime.now()),
"user-agent": userAgent,
"Cache-Control": "no-cache",
"Accept": "application/json",
"Host": _host,
"x-ms-documentdb-partitionkey": pKey == null ? "" : "[\"" + pKey_Filter + "\"]",
"x-ms-documentdb-query-enablecrosspartition": pKey == null ? "True" : ""
};
if(moreHeaders != null) {
headers.addAll(moreHeaders);
}
String strSELECT = "";
requiredColHeaders.forEach((col) {
strSELECT = strSELECT + "$azTblName[\"$col\"],";
});
strSELECT = strSELECT.substring(0, strSELECT.length - 1); // remove trailing ","
String strWHERE = "";
if(mapWHERE != null) {
mapWHERE.forEach((col, value) {
strWHERE = strWHERE + "($azTblName[\"$col\"] = \"$value\")AND";
});
strWHERE = strWHERE.substring(0, strWHERE.length - 3); // remove trailing "AND"
}
String strQuery = "SELECT $strSELECT FROM $azTblName";
if(strWHERE != "") {
strQuery = strQuery + " WHERE $strWHERE";
}
Map<String, dynamic> body = {
"query": strQuery,
"parameters": []
};
var response = await httpRequest(_collectionUrl + "/docs", headers, body, isQuery: true);
return response;
}
Future<HttpClientResponse> httpRequest(String url, Map headers, Map body, {bool isQuery = false}) async {
HttpClient httpClient = new HttpClient();
HttpClientRequest request = await httpClient.postUrl(Uri.parse(url));
headers.forEach((key, value) {
request.headers.set(key,value);
});
if(isQuery) {
request.headers.set('content-type', 'application/query+json');
request.headers.set('x-ms-documentdb-isquery', 'True');
}
if(body != null) {
request.add(utf8.encode(json.encode(body)));
}
HttpClientResponse response = await request.close();
httpClient.close();
return response;
}
//Generates authorization token
String generateAuthToken(String verb, String resourceType, String resourceLink, String date, String key, String keyType, {String tokenVer="1.0"}) {
String payload=verb.toLowerCase()+"\n"
+resourceType.toLowerCase()+"\n"
+resourceLink+"\n"
+date.toLowerCase()+"\n"
+""+"\n";
var key_decode = base64.decode(key);
Hmac hmac = new Hmac(sha256, key_decode);
var messageBytes = utf8.encode(payload);
Digest digest = hmac.convert(messageBytes);
String signature = base64.encode(digest.bytes);
return Uri.encodeComponent("type="+keyType+"&ver="+tokenVer+"&sig="+signature);
}
}
关于rest - Http.post 在flutter/dart 中不接受内容类型application/query+json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61501765/
以下哪一个更好(EJB 3 JPA) //查询 一个)。 getEntityManager().createQuery("select o from User o"); //命名查询,其中 findA
也许其他人和我有同样的问题。我遇到了错误: Cannot execute queries while other unbuffered queries are active.Consider usin
我的代码 package com.tl666.elasticsearch.pojo; import lombok.AllArgsConstructor; import lombok.Data; imp
简短版:我想查询另一个查询的结果,以便选择更有限的结果集。但是,添加 where 子句会重写第一个查询而不是处理结果,因此我得不到我需要的答案。 详情:我有两个模型,支票和蜱虫。检查 has_many
我正在尝试使用 Doctrine 在 Symfony 框架中执行原始查询。 这是代码: class MessagesHandler { /** @var \Doctrine\Common\Pe
我正在运行以下两个语句: 首先是 A) 它做它需要做的事情并工作: SELECT itemColumn ,valueColumn ,label FROM rstCombinedChartD
我有一个脚本来查询数据库以获取订单信息,然后查询该查询以获取订单总数。代码看起来像这样。 SELECT oi.OrderQty, oi.ItemPrice FROM Ord
这个问题在这里已经有了答案: MySQL Insert query doesn't work with WHERE clause (31 个答案) 关闭 4 年前。 我正在从 php 更新数据库中的
在使用 Skygear JS SDK 时,查询是否返回数组? readDummy: function(){ const Test = skygear.Record.extend('
我想在一个表上运行 MySQL 查询,然后在该表上运行子查询。我有一个对象列表。每个对象都有一个主要版本和一个次要版本。对于一个对象,我试图找到该对象的“最后版本”:这意味着我想找到该对象的最大值(主
我正在尝试在 pod 中启动 prometheus,并在 k8s 中使用持久卷。 当我启动 pod 时,我看到: level=info ts=2021-09-12T13:58:13.120Z ca
基本上,我从 kube-prometheus-stack 安装了 Prometheues-Grafana使用提供的 helm chart repo prometheus-community # hel
是否可以根据另一个查询的结果在 TFS 2010 中创建新查询? 例如,一个(父)查询选择位于某个工作项下的所有工作项(假设 ID=5 的工作项)。现在我想创建其他查询,从第一个查询的结果中选择所有错
在 Delphi 中,每当我使用 TQuery 对数据库执行 SELECT 时,我都会在 Query.Open 后面加上 try..finally,并在finally 部分中使用 Query.Clos
我只是从一台服务器移动到另一台服务器。我的脚本在旧服务器上运行良好,但是我开始在新服务器上收到此错误: "Declaration of ezSQL_mysql::query() should be c
我想问一下有什么区别 for row in session.Query(Model1): pass 和 for row in session.Query(Model1).all():
如何使用注释通过spring-data-elasticsearch进行@Query(value =“{” query“:”“}”)的聚合? 最佳答案 您不能使用@Query注释来完成此操作,该注释的唯
我有一个对可变字符串执行 LIKE 条件的查询: 当变量包含一个包含单引号的单词时,返回一些结果,但不是全部: SELECT ID FROM MyQoQ
我有我的查询范围,它返回数百条记录。我需要在 Controller 中使用不同的过滤器查询这个集合。 我怎样才能做到这一点?可能吗? 查询范围: Client::join('transactions_
我有这样的数据库模式 用户 编号 初中生 文档 编号 标题 user_id(用户的外键) 模式(可以接受 PUBLIC 或 PRIVATE) 我想检索所有公开的文档和属于给定用户(矩阵)的所有文档 我
我是一名优秀的程序员,十分优秀!