我有一個非常簡單的場景,我無法工作。我正嘗試使用cURL將JSON字符串發佈到RESTful結點,以便通過服務器上的HTTPS和RESTeasy發送請求。RESTeasy使用JSON體POST - HTTP 100
我捲曲POST正是如此配置:
$ch = curl_init();
$content = json_encode($validJsonString);
curl_setopt($ch, CURLOPT_URL, 'https://foobar.com/test?trackingId=12345');
curl_setopt($ch, CURLOPT_POSTFIELDS, array('json' => $content));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$headers = array(
'Content-Type: application/json',
'Content-Length: ' . strlen($content)
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec ($ch);
$httpResponseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$json = json_decode($response);
curl_close ($ch);
return $json;
而且RestEasy的recipiant樣子:
@POST
@Path("/test")
@Produces("application/json")
public String addObjectCommentAsJSON( @FormParam("json") String validJsonString,
@QueryParam("trackingId") String trackingId) {
Gson gson = new Gson();
SomeObject someObject = gson.fromJson(validJsonString, SomeObject.class);
String responseString = methodThatReturnsAJsonString(trackingId, someObject);
return responseString;
}
我有這個問題是該請求掛起約30秒,然後返回一個HTTP 100響應。據我所知,HTTP 100意味着繼續處理其餘的請求,但我不明白我應該怎麼做。
我試圖刪除Content-Length頭,但顯然我得到了411(需要內容長度),所以這也不是一個選項。
這可能是內容類型的問題嗎?任何幫助深表感謝。
可能的重複http://stackoverflow.com/questions/4271621/php-curl-post-json – cmbuckley 2012-03-28 22:49:45
根據php.net,如果你傳遞一個數組到'CURLOPT_POSTFIELDS'它被編碼爲'multipart/form-data '。如果直接傳遞json字符串會發生什麼? – abraham 2012-03-28 22:50:21
@cbuckley我看到了,但沒有提到RESTeasy在另一方面,這是50%的潛在問題在這裏... – travega 2012-03-28 23:02:56