我需要使用JSON对象作为正文进行POST请求。这两种方法都给我HTTP 500服务器错误。我的代码有什么明显的错误吗?要温柔... 我尝试了几种方法,包括
$checkfor = ("'serverId':'Server','featureId':'Feature','propertyId':'Property'");
$checkforJson = json_encode($checkfor);
$uri = "http://localhost:8080/v1/properties";
$response = \Httpful\Request::post($uri)
->method(Request::post)
->withoutStrictSsl()
->expectsJson()
->body($checkforJson)
->send();
pre($response);
使用HTTPful资源。我已经尝试使用cURL
$service_url = "http://localhost:8080/v1/properties";
// Initialize the cURL
$ch = curl_init($service_url);
// Set service authentication
// Composing the HTTP headers
$body = array();
$body[] = '"serverId" : "Server"';
$body[] = '"featureId" : "Feature"';
$body[] = '"propertyId" : "Property"';
$body = json_encode($body);
$headers = array();
$headers[] = 'Accept: application/xml';
$headers[] = 'Content-Type: application/xml; charset=UTF-8';
// Set the cURL options
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
// Execute the cURL
$data = curl_exec($ch);
// Print the result
pre($data);
答案 0 :(得分:2)
您的json_encode需要一个数组。
它应该看起来像这样
<?php
$checkfor = ([
'serverId'=>'Server',
'featureId'=>'Feature',
'propertyId'=>'Property'
]);
$checkforJson = json_encode($checkfor);
var_dump($checkforJson); // this will now work
为更好地理解,请阅读 doc
更新我也注意到curl脚本,您的数组需要再次修复
$body['serverId'] = 'Server';
,然后不对发布字段进行json编码,它需要一个数组。
答案 1 :(得分:2)
我前段时间有类似的问题。
对我有用的解决方案是:
$url = 'http://yourURL.com/api';
$data = array('field1' => 'value', 'field2' => 'value');
$options = array(
'http' => array(
'header' => "Content-type: application/json\r\n",
'method' => 'POST',
'content' => json_encode($data),
)
);
$context = stream_context_create($options);
$result = file_get_contents( $url, false, $context );
$response = json_decode( $result );
可以找到类似的答案HERE
答案 2 :(得分:1)
您尝试过吗:
Observable<R> obs = Observable.fromCallable(this::work);
也许是您设置阵列的方式