DataSnap Server HTTP json格式修改 返回图片
{"result":["hello"]}
GetCommandResponse>DSHTTPService1FormatResult>(if not Handled then) GetCommandResponse
1、去掉result符号
能否只返回["hello"] ,不加result标识,和tcp/IP返回的结果一样?
procedure TServerContainer1.DSHTTPService1FormatResult(Sender: TObject;
var ResultVal: TJSONValue; const Command: TDBXCommand; var Handled: Boolean);
var
str: string;
begin
// str :=ResultVal.ToString;
Handled := true;//加上这句话就可以了。
end;
在Datasnap.DSHTTPCommon.pas,有
procedure TDSJsonResponseHandler.GetCommandResponse
if not Handled then
ResponseObj := TJSONObject.Create(TJSONPair.Create(TJSONString.Create('result'), JsonParam));的处理,所以可以屏蔽。Handled 设为true就好了。
还有URL编码,特别是汉字,如何能禁止呢??再查。
2、去掉转义字符
元凶在这Datasnap.DSHTTPCommon.pas,TDBXJSONTools.DBXToJSON,
'["[{\"PID\":\"0001\",\"Name\":\"\u5F20\u4E09\u4E30\",\"Sex\":\"\u7537\",\"Age\":100},{\"PID\":\"002\",\"Name\":\"\u90ED\u9756\",\"Sex\":\"\u7537\",\"Age\":80}]"]'
JsonParam := TDBXJSONTools.DBXToJSON(Command.Parameters[I].Value,
Command.Parameters[I].DataType, FDSService.LocalConnection);
//用下面这句话就可以了,DBXToJSON引起的。
JsonParam := TJSONObject.ParseJSONValue(Command.Parameters[I].Value.AsString) as TJSONValue;
TJSONString
2016.10.11 做了二次修改,兼容单个键值,数值值。例如,进返回时间["2012.0.0.0."]
procedure TDSJsonResponseHandler.GetCommandResponse(Command: TDBXCommand; out Response: TJSONValue;
out ResponseStream: TStream);
var
JsonParams: TJSONArray;
JsonParam,JsonParamBak,jv: TJSONValue;
I: Integer;
ParamDirection: TDBXParameterDirection;
OwnParams: Boolean;
ConvList: TObjectList<TDBXRequestFilter>;
ResponseObj: TJSONObject;
Handled: Boolean;
tstr:string;
strStream:TStringStream;
useDefault:Boolean;
begin
JsonParams := nil;
OwnParams := false;
ConvList := nil;
ResponseStream := nil;
Handled := False; try
// collect the output/return parameters
JsonParams := TJSONArray.Create;
OwnParams := true;
useDefault:=false;
ConvList := TObjectList<TDBXRequestFilter>.Create(false); for I := to Command.Parameters.Count - do
begin
// select the output and return parameters for the response
ParamDirection := Command.Parameters[I].ParameterDirection;
if (ParamDirection = TDBXParameterDirections.OutParameter) or
(ParamDirection = TDBXParameterDirections.InOutParameter) or
(ParamDirection = TDBXParameterDirections.ReturnParameter) then
begin
JsonParam := nil;
ConvList.Clear; {If a subclass doesn't handle the parameter themselves, then manage the parameter by either
applying a filter on the result, or passing back the pure JSON representation}
if not HandleParameter(Command, Command.Parameters[I], JsonParam, ResponseStream) then
begin
FDSService.FiltersForCriteria([I], I = Command.Parameters.Count - , ConvList);
if ConvList.Count = then
begin
if not ConvList.Items[].CanConvert(Command.Parameters[I].Value) then
Raise TDSServiceException.Create(Format(SCannotConvertParam, [I, ConvList.Items[].Name]));
JsonParam := ConvList.Items[].ToJSON(Command.Parameters[I].Value, FDSService.LocalConnection);
end
else
begin
JsonParam := TJSONObject.ParseJSONValue(Command.Parameters[I].Value.AsString) as TJSONValue;
if JsonParam=nil then
begin
useDefault:=true;
JsonParam := TDBXJSONTools.DBXToJSON(Command.Parameters[I].Value,
Command.Parameters[I].DataType, FDSService.LocalConnection);
end;
end;
end; if JsonParam <> nil then
JsonParams.AddElement(JsonParam);
end;
end; //Store the result array as a JSON Value, to make it more generic for the event and result object
JsonParamBak:=JsonParam;
JsonParam := JsonParams; if Assigned(FResultEvent) then
FResultEvent(Self, JsonParam, Command, Handled); if not Handled then
begin
ResponseObj := TJSONObject.Create(TJSONPair.Create(TJSONString.Create('result'), JsonParam));
//allow subclasses to add to the result object if they wish
ProcessResultObject(ResponseObj, Command);
Response := ResponseObj;
end
else
begin
if (JsonParams.Count = ) and (useDefault=false) then
Response := JsonParamBak
else
Response := JsonParam;
end; OwnParams := false;
finally
FreeAndNil(ConvList);
if OwnParams then
JsonParams.Free;
end;
end;
GetCommandResponse
3、 防止汉字转成unicode数字
[{"PID":"0001","Name":"张三丰","Sex":"男","Age":100},{"PID":"002","Name":"郭靖","Sex":"男","Age":80}]
通过浏览器打开URL,返回的是
[[{"PID":"0001","Name":"\u5F20\u4E09\u4E30","Sex":"\u7537","Age":100},{"PID":"002","Name":"\u90ED\u9756","Sex":"\u7537","Age":80}]]
Datasnap.DSHTTP.pas function PopulateContent
ResponseInfo.ContentText := StringOf(ByteContent(Response))
改为:
ResponseInfo.ContentText := Response.ToString;
Response.Value 为空的原因是结构不一致不,从tjson读取后看value不为空,那么返回value测试一下不。或者对于 Command.Parameters[I].DataType是字符的特殊处理。
CharSet
IdCustomHTTPServer.pas
FConnection.IOHandler.Write(ContentText, CharsetToEncoding(CharSet));
IdCustomHTTPServer.pas
ResponseInfo.ContentText 这个就是返回字符串,
ResponseInfo.ContentText := Response.ToString;// Response.ToJSON;
ResponseInfo.ContentText := AnsiReplaceStr(ResponseInfo.ContentText, '\"','"');
还是因为这个引起的,System.JSON.pas
function TJSONString.ToString: string;
begin
if FStrBuffer = nil then
Result := ''
else
Result := '"' + AnsiReplaceStr(FStrBuffer.ToString, '"', '\"') + '"';
end;
var
jo: TJSONObject;
Command: TDBXCommand;
pa: TDBXParameter;
JsonParam: TJSONValue;
begin pa := TDBXParameter.Create(nil);
pa.DataType := TDBXDataTypes.AnsiStringType;
pa.Value.AsString := '[{"name":"david"}]'; JsonParam := TDBXJSONTools.DBXToJSON(pa.Value, pa.DataType, false);
self.Caption := JsonParam.Value;
JsonParam.ToString;
4、字符编码
text/html与text/plain
在浏览器打开的时候汉字是乱码,查看网页属性,编码是windows-132,而asp。net生成的webapi网页是utf-8,查找IdCustomHTTPServer.pas
procedure TIdHTTPResponseInfo.WriteHeader;
var
i: Integer;
LBufferingStarted: Boolean;
begin
if HeaderHasBeenWritten then begin
raise EIdHTTPHeaderAlreadyWritten.Create(RSHTTPHeaderAlreadyWritten);
end;
FHeaderHasBeenWritten := True; if AuthRealm <> '' then
begin
ResponseNo := ;
if (Length(ContentText) = ) and (not Assigned(ContentStream)) then
begin
ContentType := 'text/html; charset=utf-8'; {Do not Localize}
ContentText := '<HTML><BODY><B>' + IntToStr(ResponseNo) + ' ' + ResponseText + '</B></BODY></HTML>'; {Do not Localize}
ContentLength := -; // calculated below
end;
end; // RLebeau 5/15/2012: for backwards compatibility. We really should
// make the user set this every time instead...
if ContentType = '' then begin
if (ContentText <> '') or (Assigned(ContentStream)) then begin
ContentType := 'text/html; charset=ISO-8859-1'; {Do not Localize}//ContentType := 'text/plain; charset=utf-8';
end;
end;
字符编码是charset=ISO-8859-1,改成utf-8就好了!!!
给TDBXParameter赋值
procedure TForm4.Button1Click(Sender: TObject);
var
LDBXParameter: TDBXParameter;
LJSONParam: TJSONValue;
pin: TDBXParameter;
begin
LDBXParameter := TDBXParameter.Create(); pin := TDBXParameter.Create();
pin.DataType := TDBXDataTypes.AnsiStringType;
pin.Value.AsString := '[{"PatientID":"aaaaaaaaa","PictuerName":"BBBBBBBBBBB"}]';
LJSONParam := TDBXJSONTools.DBXToJSON(pin.Value, pin.DataType, false);
//下面这句直接赋值给LJSONParam 不行,导致下面的JSONToDBX转换失败,所以改用上面的方法DBXToJSON
// LJSONParam := TJSONObject.ParseJSONValue('[{"PatientID":"aaaaaaaaa","PictuerName":"BBBBBBBBBBB"}]'); LDBXParameter.DataType := TDBXDataTypes.WideStringType;
TDBXJSONTools.JSONToDBX(LJSONParam, LDBXParameter.Value, LDBXParameter.DataType, true, true (* Owns *) );
Memo1.Lines.Text := LDBXParameter.Value.AsString end;
Datasnap.DSService.pas
procedure TDSRESTService.ProcessParameters(const ADSMethodName: string; const Params: TStrings; Content: TArray<Byte>; const ACommand: TDBXCommand);
dataSnap服务器获取get、post方法参数
URL格式
http://localhost:8023/datasnap/rest/TServerMethods1/EchoString?p1=p1325325&p2=anme
uses Data.DBXPlatform;
function TServerMethods1.EchoString(Value: string): string;
begin
p1 := GetInvocationMetadata.QueryParams.Values['p1'];
p2 := GetInvocationMetadata.QueryParams.Values['p2'];
end;
这样只能获取到列表的参数,无法获取流参数。
获取POST方法
把方法名加update前缀
function TServerMethods1.updateEchoString(Value: string): string;
begin
p1 := GetInvocationMetadata.QueryParams.Values['p1'];
p2 := GetInvocationMetadata.QueryParams.Values['p2'];
Value就是post里的字符串
end;
RESTDebug发送汉字编码处理(未使用utf8编码)
RESTDebug,发送汉字,TEncoding.ANSI.GetString(Content)可以接收汉字。如下修改正常了。 Datasnap.DSService.pas文件。原版代码接收到的是乱码。
var
as1:AnsiString;
utf8Bytes:TBytes;
as1 := TEncoding.ANSI.GetString(Content);
utf8Bytes:=TEncoding.UTF8.GetBytes(as1);
LBody := TJSONObject.ParseJSONValue(utf8Bytes, 0,True);
如果汉字经过ut8编码0x,比如在线工具里的utf8编码0x0x0x则,接收正常,无需任何改动。
用http://ouapi.com/在线post工具发送明文汉字
datasnap服务器接收乱码。
返回数据自己修改
2018.1.18
想返回什么格式都行啦!!!
function TServerMethods1.EchoString(Value: string): string;
begin
Result := Value;
GetInvocationMetadata.ResponseContent:='abc';
end;
like this
http://blog.csdn.net/maxwoods/article/details/25163357
https://stackoverflow.com/questions/13879238/return-an-image-from-a-delphi-rest-server-and-show-it-in-a-browser/13879240#13879240
还可以设置返回的格式,这样的话不需要任何修改了,不需要修改原文件,各种跨平台前端都支持!
GetInvocationMetadata.ResponseContentType := 'image/png';
GetInvocationMetadata.ResponseContentType := 'image/jpeg';
GetInvocationMetadata.ResponseContentType := 'application/json';
GetInvocationMetadata().ResponseContentType := 'application/json; charset=utf-8';
GetInvocationMetaData.CloseSession := True;
thanks
http://www.itgo.me/a/x5918832448088122623/how-to-download-jpeg-image-for-browsers-in-delphi-c-builder-rest-webbroker
void TCommerMethods::DownloadImage()
{
TStringStream *Stream;
String S;
Stream = new TStringStream;
Stream->LoadFromFile("C:\\Temp\\MyImage.jpg");
S = Stream->DataString;
delete Stream;
GetInvocationMetadata()->ResponseContentType = "image/jpeg";
GetInvocationMetadata()->ResponseContent = S;
}
https://pastebin.com/tgiNep52
function CarregarImagem(const sCaminho: String): AnsiString;
var
oFileStream : TFileStream;
begin
oFileStream:= TFileStream.Create(sCaminho, fmOpenRead or fmShareDenyWrite);
try
if oFileStream.Size > then
begin
SetLength(Result, oFileStream.Size);
oFileStream.Read(Pointer(Result)^, oFileStream.Size);
end;
finally
FreeAndNil(oFileStream);
end; sImagem := CarregarImagem(sDirImagem);
if (bExtPNG) then
GetInvocationMetadata().ResponseContentType := 'image/png'
else
begin
GetInvocationMetadata().ResponseContentType := 'image/jpeg';
GetInvocationMetadata().ResponseCode := ;
GetInvocationMetadata().ResponseContent := sImagem;
GetInvocationMetadata().CloseSession := True;
end;
http://kelvermerlotti.com/por-que-o-datasnap-rest-retorna-um-json-sujo/#more-203
procedure TWebModule1.DSHTTPWebDispatcher1FormatResult(Sender: TObject;
var ResultVal: TJSONValue; const Command: TDBXCommand; var Handled: Boolean);
var
Aux: TJSONValue;
begin
Aux := ResultVal;
ResultVal := TJSONArray(Aux).Items[0];
TJSONArray(Aux).Remove(0);
Aux.Free;
Handled := True;
end
DataSnap Server HTTP json格式修改 返回图片的更多相关文章
- struts2使用jsp和<s:property>标签获取json格式的返回数据
struts2使用jsp和<s:property>标签获取json格式的返回数据 1.struts2的action中 return "success"; 2.指向的返回 ...
- 关于C# webapi ,接口返回字符串和json格式 ,返回值中有反斜杠
最近遇到一个比较郁闷的问题,记录一下 写了一个接口,想返回json 数据,但是返回值中总是带有反斜杠... ,下面来看原因 首先,配置 webapi的路由 App_Start 文件夹下 ,WebApi ...
- JSON API免费接口 各种提供JSON格式数据返回服务网站的API接口
这里为大家搜集了一些能够返回JSON格式的服务接口.部分需要用JSONP调用. 电商接口 京东获取单个商品价格接口: http://p.3.cn/prices/mgets?skuIds=J_商品ID& ...
- @ResponseBody将集合数据转换为json格式并返回给客户端
spring-mvc.xml: <beans xmlns:mvc="http://www.springframework.org/schema/mvc" > <m ...
- ReadyAPI/soapUI发送post请求json格式(带有中文字符),后台获取参数为空
解决:请求编码格式默认为空,在"TestCase"的指定Step的Request Properties中, 改Encoding编码格式为UTF-8. 原文:soapUI发送post ...
- Django-choices字段值对应关系(性别)-MTV与MVC科普-Ajax发json格式与文件格式数据-contentType格式-Ajax搭配sweetalert实现删除确认弹窗-自定义分页器-批量插入-07
目录 models 字段补充 choices 参数/字段(用的很多) MTV与MVC模型 科普 Ajax 发送 GET.POST 请求的几种常见方式 用 Ajax 做一个小案例 准备工作 动手用 Aj ...
- html中通过js获取接口JSON格式数据解析以及跨域问题
前言:本人自学前端开发,一直想研究下js获取接口数据在html的实现,顺利地找到了获取数据的方法,但是有部分接口在调用中出现无法展示数据.经查,发现时跨域的问题,花费了一通时间,随笔记录下过程,以方便 ...
- java请求POST发送json格式请求
public static String upload(String url){ try { HttpClient httpclient = new DefaultHttpClient(); Http ...
- 快速将对象转化为JSON格式
1.导入阿里巴巴fastjson包. <!-- fastJson将对象转化为Json对象 --> <dependency> <groupId>com.alibaba ...
随机推荐
- gem install没有反应 解决办法
在虚拟机上执行gem install redis 没有反应: 百度方法如下: #移除官方镜像,增加淘宝镜像 gem sources --add https://ruby.taobao.org/ --r ...
- 设置tab标签页 遮挡部分
效果如下: 主要代码: <div class="need-detail"> <div class="top-title"> <sp ...
- Eclipse使用前准备(转)
Eclipse的发布流程 M1 08/19/2009 M2 09/30/2009 M3 11/11/2009 M4 12/16/2009 M ...
- gridview 自动序号 合计
第一种方式,直接在Aspx页面GridView模板列中.这种的缺点是到第二页分页时又重新开始了. <asp:TemplateField HeaderText="序号" Ins ...
- XtraForm
禁用窗体大小变化 this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D; Note:设置成FixedSingle是无效 ...
- Django中更新多个对象数据与删除对象的方法
更新多个对象 例如说我们现在想要将Apress Publisher的名称由原来的”Apress”更改为”Apress Publishing”.若使用save()方法,如: ? 1 2 3 >&g ...
- hadoop技术入门学习之发行版选择
经常会看到这样的问题:零基础学习hadoop难不难?有的人回答说:零基础学习hadoop,没有想象的那么难,也没有想象的那么容易.看到这样的答案不免觉得有些尴尬,这个问题算是白问了,因为这个回答似乎什 ...
- mysql实战优化之九:MySQL查询缓存总结
mysql Query Cache 默认为打开.从某种程度可以提高查询的效果,但是未必是最优的解决方案,如果有的大量的修改和查询时,由于修改造成的cache失效,会给服务器造成很大的开销. mysql ...
- java实验——将100-300之间的素数输出
public class 实验1 { //将100-300之间的素数输出 /** * @param args */ public static void main(String[] args) { / ...
- select count(*) as total from(select count(*) from tab_cb_casim group by `card_no`) as cai;
子查询必须加一个别名才能执行!!