错误HTTP状态415 - 将XML从php发送到RESTful时不支持的媒体类型

错误HTTP状态415  - 将XML从php发送到RESTful时不支持的媒体类型

问题描述:

I have RESTful web service deploy on jboss EAP. In other, I create php file for make a XML and I want send(POST) them to RESTful.

The RESTful http://192.168.0.191:8080/UserManagement/rest/UserService/users/ webservice show like this :

<sample>
  <user>
    <id>1</id>
    <name>Moyes Chuck</name>
    <profession>Teacher</profession>
  </user>
  <user>
    <id>2</id>
    <name>Van Gaal</name>
    <profession>Driver</profession>
  </user>
</sample>

And create postxml.php :

$xml = new SimpleXMLElement('<sample/>');
    $track = $xml->addChild('user');
    $track->addChild('id', "3");
    $track->addChild('name', "Brody Ben");
    $track->addChild('profession', "Manager");

in same file(postxml.php), this code I wrote to post to RESTful:

$service_url1 = 'http://192.168.0.191:8080/UserManagement/rest/UserService/users/';
$curl1 = curl_init($service_url1);
curl_setopt($curl1, CURLOPT_RETURNTRANSFER, true);
$arr=array("key"=>$xml);
curl_setopt($curl1, CURLOPT_POST, 1);
curl_setopt($curl1, CURLOPT_POSTFIELDS,$arr);
echo $curl1_response = curl_exec($curl1);
curl_close($curl1);

But when I run postxml.php on browser http://localhost:82/test3/postxml.php, it show an error :

HTTP Status 415 - Unsupported Media Type.
JBWEB000069: description JBWEB000135: The server refused this request because the request entity is in a format not supported by the requested resource for the requested method.

我在jboss EAP上部署了RESTful Web服务。 在其他方面,我创建用于生成XML的php文件,我想将它们发送(POST)到RESTful。

RESTful http://192.168.0.191:8080/UserManagement/rest/UserService/users/ webservice如下所示: p>

 &lt; sample&gt; 
&lt; user&gt; 
&lt; id&gt; 1&lt; / id&gt; 
&lt; name&gt; Moyes Chuck&lt; / name&gt; 
&lt; profession&gt; Teacher&lt; / profession&gt; 
&lt;  / user&gt; 
&lt; user&gt; 
&lt; id&gt; 2&lt; / id&gt; 
&lt; name&gt; Van Gaal&lt; / name&gt; 
&lt; profession&gt; Driver&lt; / profession&gt; 
&lt; / user&gt;  
&lt; / sample&gt; 
  code>  pre> 
 
 

并创建postxml.php: p>

  $ xml = new SimpleXMLElement('  &lt; sample /&gt;'); 
 $ track = $ xml-&gt; addChild('user'); 
 $ track-&gt; addChild('id',“3”); 
 $ track-&gt  ; addChild('name',“Brody Ben”); 
 $ track-&gt; addChild('profession',“Manager”); 
  code>  pre> 
 
 

in file(postxml.php),这段代码写给了p ost到RESTful: p>

  $ service_url1 ='http://192.168.0.191:8080/UserManagement/rest/UserService/users/';
$curl1 = curl_init($  service_url1); 
curl_setopt($ curl1,CURLOPT_RETURNTRANSFER,true); 
 $ arr = array(“key”=&gt; $ xml); 
curl_setopt($ curl1,CURLOPT_POST,1); 
curl_setopt($ curl1,CURLOPT_POSTFIELDS,  $ arr); 
echo $ curl1_response = curl_exec($ curl1); 
curl_close($ curl1); 
  code>  pre> 
 
 

但是当我在浏览器 http:// localhost:82 / test3 / postxml.php ,显示错误: p>

HTTP状态415 - 不支持的媒体类型。
JBWEB000069:说明JBWEB000135:服务器拒绝此请求,因为请求实体的格式不受请求的资源支持 请求的方法。 p> blockquote> div>

remove $arr=array("key"=>$xml); and just add this code and will be work fine

curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/xml',                                                                                
    'Content-Length: ' . strlen($xml))                                                                       
); 

You are POSTing an object of type SimpleXMLElement to the web service.

Send the xml string instead

$arr=array("key"=>$xml->asXML());

If try

print $xml->asXML();

You will see the actual XML string, which is what you need to send to the web service.