如何使用method :: http :: geturl -method PUT输出xml内容

时间:2014-04-20 04:48:58

标签: http tcl alm

当我尝试使用Fire Fox Open HttpRequest Addon时,我能够成功地进行操作。

PUT http://12.222.20.17:8080/qcbin/rest/domains/test/projects/runtest/runs/385762
Accept: application/xml
Content-Type: application/xml
<Entity Type="run"> 
    <Fields> 
         <Field Name="status"> 
               <Value>Passed</Value> 
         </Field> 
     </Fields> 
</Entity>

 -- response --
200 OK
Server:  Apache-Coyote/1.1

但是我试图用TCL PUT方法http包来模拟相同的操作,我收到错误的请求响应

set xml {<Entity Type="run"><Fields><Field Name="status"><Value>Passed</Value></Field></Fields></Entity>}
set Headers(Cookie) $cookie
set Headers(Accept) "application/xml"
set Headers(Content-Type) "application/xml"
set Headers(Content) $xml
set token [::http::geturl "http://12.222.20.17:8080/qcbin/rest/domains/test/projects/runtest/runs/385762" -method PUT -headers [array get Headers]]

响应

% ::http::data $token
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><QCRestException><Id>qcco
re.general-error</Id><Title>Bad Request</Title><StackTrace>javax.ws.rs.WebApplic
ationException: com.ctc.wstx.exc.WstxEOFException: Unexpected EOF in prolog&#xD;

 at [row,col {unknown-source}]: [1,0]&#xD;
        at org.apache.wink.common.internal.providers.entity.xml.JAXBXmlProvider.
readFrom(JAXBXmlProvider.java:113)&#xD;
        at org.apache.wink.server.internal.registry.ServerInjectableFactory$Enti
tyParam.getValue(ServerInjectableFactory.java:190)&#xD;

我不确定我遗失的地方,请帮助解决此问题。

由于

1 个答案:

答案 0 :(得分:0)

要通过PUT在请求中发送XML文档,假设您已将文档序列化为可通过$theXML获取的字符串,则只需执行此操作:

# Type *might* need to be text/xml
http::geturl $theServiceURL -method PUT -type application/xml -query $theXML

当然,您必须确保处理它正确返回的令牌,依此类推。 http包仍然很低级。 (例如,您可能必须使用http::config来设置您在响应中获得的MIME类型,并且您需要自己处理会话的重定向和Cookie。)


发送JSON代替?然后类型变为application/json。简单!