2011-07-13 133 views
1

我需要測試以下規格捲曲命令測試API

URL: http://myapp.com/api/upload/ 
Method: POST 
Parameters: 
    image: (binary data of image) 

returns 
    a JSON Object 

樣品PHP執行測試API

<?php 
$ch = curl_init(); 
$url = 'http://myapp.com/api/upload/'; 

$fields = array('image'=>file_get_contents('profile-pic.jpg')); 

curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); 
curl_setopt($ch, CURLOPT_URL, $url); 

$json = curl_exec($ch); 
echo "\n".$json."\n"; 

?> 

我使用RESTClient實現和海報擴展測試其他API的API,我不知道如何使用這些工具發送名爲image的參數的二進制數據。

如何使用Curl命令測試上述api?

沒有運氣這一個

curl -X POST --data-binary "[email protected]" "http://myapp.com/api/upload/" 

test.jpg是在當前目錄。我無法更改第三方API,以便能夠使用其他編碼格式進行測試。

回答

1

CURLOPT_POSTFIELDS與PHP中的哈希數組對應的命令行-F選項。因此,它應該是類似於:

curl -F [email protected] http://myapp.com/api/upload/ 

如果不是確切的事情,加上「--trace-ascii的dump.txt」,以獲得一個完整的跟蹤記錄,並檢查了密切的,看看有什麼錯誤。

+1

感謝您的--trace-ascii我沒有意識到這一點。 +1 – palaniraja