0

无论我设置什么,以下请求始终采用默认内容类型。我设置为 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 -> 无内容类型集

4

1 回答 1

0

你是如何检查默认的 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>
于 2017-11-17T00:11:01.990 回答