idHttp:如何处理json请求和响应 [英] IdHttp : how to deal with json request and response

查看:91
本文介绍了idHttp:如何处理json请求和响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一些使用 JSON 进行请求和响应的网站
我碰到两种类型:
1- application/x-www-form-urlencoded 作为请求并返回响应 application/json 内容类型
请求和响应的 2- application/json 内容类型
type 1 中,我尝试使用
更改响应内容的类型 mIdHttp.Response.ContentType:='application/json';
但是使用http分析器我可以看到它没有变化,并且仍然是 text/html
现在我不知道问题是否在于我无法更改内容类型,但我不知道如何处理 json
有关 json 的几个问题:
1-发布时我是否必须对json数据进行编码?怎么样?
2-我如何解析json响应代码?怎么得到的?是否需要某种编码或特殊转换?
3-每种类型的json的idhttp设置随每个站点而变化,需要进行配置吗?

我了解我的问题听起来有些笼统,但所有其他问题都非常具体,在处理'application/json'内容类型时不介绍基本知识.


感谢 Remy Lebeau 的回答,我能够成功使用 type 1
但我仍然无法发送 JSON 请求,有人可以分享一个有效的示例吗,这是发布信息的网站之一,请在您的示例中使用它:

重要提示::该特定站点的帖子和请求内容完全相同!它使我感到困惑,因为在该站点上,我指定了开始日期结束日期,然后单击文件夹(如图标)和此 post 已发送(您可以在上面看到),并且 result 应该是 links (确实是)但是不仅只出现在请求内容中,它们还出现在 post 中!(同样,我也在尝试获取链接,但是在 post 链接上,我想要的东西也正在发送,我该如何张贴我没有的东西!!)

为了更加清楚,这是我填写日期和提到的图标的地方:

解决方案

您不能指定响应的格式,除非所请求的资源为此目的提供了明确的输入参数或专用URL(例如,请求响应是以html,xml,json等格式发送).设置 TIdHTTP.Response.ContentType 属性是没有用的.它将被响应的实际 Content-Type 标头覆盖.

要在请求中发送 JSON,必须将其发布为 TStream ,例如 TMemoryStream TStringStream ,然后根据需要设置 TIdHTTP.Request.ContentType ,例如:

  varReqJson:TStringStream;开始ReqJson:= TStringStream.Create('json content here',TEncoding.UTF8);尝试IdHTTP1.Request.ContentType:='application/json';IdHTTP1.Post(URL,ReqJson);最后ReqJson.免费;结尾;结尾; 

接收 JSON, TIdHTTP 可以

  1. 将其作为 String (使用服务器报告的字符集进行解码)返回:

      varReqJson:TStringStream;响应:字符串;开始ReqJson:= TStringStream.Create('json content here',TEncoding.UTF8);尝试IdHTTP1.Request.ContentType:='application/json';RespJson:= IdHTTP1.Post(URL,ReqJson);最后ReqJson.免费;结尾;//根据需要使用RespJson ...结尾; 

  2. 将原始字节写入您选择的输出 TStream :

      varReqJson:TStringStream;响应:TMemoryStream;开始RespJson:= TMemoryStream.Create;尝试ReqJson:= TStringStream.Create('json内容在这里',TEncoding.UTF8);尝试IdHTTP1.Request.ContentType:='application/json';RespJson:= IdHTTP1.Post(URL,ReqJson,RespJson);最后ReqJson.免费;结尾;RespJson.Position:= 0;//根据需要使用RespJson ...最后响应免费.结尾;结尾; 

HTTP响应代码在 TIdHTTP.Response.ResponseCode (和 TIdHTTP.ResponseCode )属性中可用.

I have encountered a few sites that uses JSON for request and response
I come across two types :
1- application/x-www-form-urlencoded as request and return a response application/json content type
2- application/json content type for both request and response
in type 1 i tried changing the the response content type using
mIdHttp.Response.ContentType := 'application/json';
but using http analyzer i can see that it doesn't change and its still text/html
now i do not know if the problem is with the fact that i can't change content type or not but i do not know how to deal with json !
a few question regarding json :
1- do i have to encode json data when posting ? how ?
2- how can i parse the json response code ? how to get it ? does it need some kind of encode or special convertion ?
3- what kind of idhttp setting for json changes with each site and needs configuring ?

I understand my questions sound a bit general, but all the other questions are very specific and don't explain the basics when dealing with 'application/json' content type.

Edit 1 :
thanks to Remy Lebeau answer i was able to successfully work with type 1
but i still have trouble sending JSON request, can someone please share an working example, this is one of the sites posting information please use this for your example :

One important note : this particular site's post and request content are exactly like each other ! and it baffles me because at the site, i specify an start date and an end date then click on a folder like icon and this post is sent (the one you can see above), and the result should be links (and it is) but instead of appearing only in request content they also appear in post ! (also i'm trying to get the links but at the post the links, the thing i want, are also being sent, how can i post something i don't have !!?)

just for more clarity here is the place i fill the date and the icon i mentioned :

解决方案

You cannot specify the format of the response, unless the requested resource offers an explicit input parameter or dedicated URL for that exact purpose (ie, to request a response be sent as html, xml, json, etc). Setting the TIdHTTP.Response.ContentType property is useless. It will be overwritten by the actual Content-Type header of the response.

To send JSON in a request, you must post it as a TStream, like TMemoryStream or TStringStream, and set the TIdHTTP.Request.ContentType as needed, eg:

var
  ReqJson: TStringStream;
begin
  ReqJson := TStringStream.Create('json content here', TEncoding.UTF8);
  try
    IdHTTP1.Request.ContentType := 'application/json';
    IdHTTP1.Post(URL, ReqJson);
  finally
    ReqJson.Free;
  end;
end;

To receive JSON, TIdHTTP can either

  1. return it as a String (decoded using a server-reported charset):

    var
      ReqJson: TStringStream;
      RespJson: String;
    begin
      ReqJson := TStringStream.Create('json content here', TEncoding.UTF8);
      try
        IdHTTP1.Request.ContentType := 'application/json';
        RespJson := IdHTTP1.Post(URL, ReqJson);
      finally
        ReqJson.Free;
      end;
      // use RespJson as needed...
    end;
    

  2. write the raw bytes to an output TStream of your choosing:

    var
      ReqJson: TStringStream;
      RespJson: TMemoryStream;
    begin
      RespJson := TMemoryStream.Create;
      try
        ReqJson := TStringStream.Create('json content here', TEncoding.UTF8);
        try
          IdHTTP1.Request.ContentType := 'application/json';
          RespJson := IdHTTP1.Post(URL, ReqJson, RespJson);
        finally
          ReqJson.Free;
        end;
        RespJson.Position := 0;
        // use RespJson as needed...
      finally
        RespJson.Free;
      end;
    end;
    

The HTTP response code is available in the TIdHTTP.Response.ResponseCode (and TIdHTTP.ResponseCode) property.

这篇关于idHttp:如何处理json请求和响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆