【问题标题】:Unable to set content type of http header in REST无法在 REST 中设置 http 标头的内容类型
【发布时间】:2017-11-16 19:49:24
【问题描述】:

无论我设置什么,以下请求始终采用默认内容类型。我设置为 application/xml,但它仍然是 application/json。但是,当我通过 Postman 调用服务时,这个问题就不存在了。

Set objHTTPRequest=##class(%Net.HttpRequest).%New()
Set objHTTPResponse=##class(%Net.HttpResponse).%New()
Do objHTTPRequest.SetHeader("Authorization", "username:password")
Do objHTTPRequest.SetHeader("Content-Type", "application/xml")
Do objHTTPRequest.Send("GET","url")
Set Op=objHTTPRequest.HttpResponse.Data.Read()

zw objHTTPRequest -> 没有设置内容类型

【问题讨论】:

  • 我不确定我是否理解,但我认为问题出在标题名称中。 Content-Type 用于请求。使用 Accept 说出你想要的回应。
  • @MarianP 也试过这个 objHTTPRequest.SetHeader("Accept", "application/xml")
  • 不知道投反对票的原因

标签: rest intersystems-cache intersystems objectscript


【解决方案1】:

您是如何检查默认 Content-Type 的?
也许您错过了一些关于 HTTP 的真正工作原理的内容?
当您使用POSTPUT 请求发送一些数据时,您可以为该数据设置Content-Type。如果你想以你想要的格式从服务器检索一些数据,你可以设置 header Accept,但是当你设置这个 header 时,并不意味着你真的以要求的格式获取这些数据,它只是取决于这个特定服务器的实现。举一些例子。

set ht=##class(%Net.HttpRequest).%New()
set ht.Server="example.org"
set ht.Port=80
set ht.Username="username"
set ht.Password="password"
#; we a going to send some xml
do ht.ContentBody.Write("<root></root>")
#; for this data we can set ContentType
set ht.ContentType="application/xml"
#; then we POST this data, where 1 it is just test flag, for debug
set sc=ht.Post("/somepath", 1)

#; As a result you can see that Content-Type has our value 
POST /somepath HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; Cache;)
Host: example.org
Accept-Encoding: gzip
Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=
Content-Length: 13
Content-Type: application/xml

<root></root>

【讨论】:

  • 使用 Postman 正确设置预期的内容类型 json 或 xml 并获取我传递的类型的数据
  • 你能把Postman的截图放上来,你想达到什么效果。正如我已经说过的,你不能为 GET 请求设置 Content-Type,它完全没用,因为你没有任何内容。您也不能使用 ZWRITE 命令检查它。只有测试标志可以帮助您查看。
猜你喜欢
  • 2019-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-17
  • 1970-01-01
  • 1970-01-01
  • 2012-05-30
相关资源
最近更新 更多