Ubuntu kullanıyorum ve üzerine cURL yükledim. Spring REST uygulamamı cURL ile test etmek istiyorum. POST kodumu Java tarafında yazdım. Ancak bunu cURL ile test etmek istiyorum. JSON bir veri göndermeye çalışıyorum. Örnek veri şu şekilde:
{"value":"30","type":"Tip 3","targetModule":"Target 3","configurationGroup":null,"name":"Configuration Deneme 3","description":null,"identity":"Configuration Deneme 3","version":0,"systemId":3,"active":true}
Bu komutu kullanıyorum:
curl -i \
-H "Accept: application/json" \
-H "X-HTTP-Method-Override: PUT" \
-X POST -d "value":"30","type":"Tip 3","targetModule":"Target 3","configurationGroup":null,"name":"Configuration Deneme 3","description":null,"identity":"Configuration Deneme 3","version":0,"systemId":3,"active":true \
http://localhost:8080/xx/xxx/xxxx
Bu hatayı döndürür:
HTTP/1.1 415 Unsupported Media Type
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=utf-8
Content-Length: 1051
Date: Wed, 24 Aug 2011 08:50:17 GMT
Hata açıklaması şöyledir:
İstek varlığı, istenen yöntem için istenen kaynak tarafından desteklenmeyen bir formatta olduğu için sunucu bu isteği reddetti ().
Tomcat günlüğü: "POST /ui/webapp/conf/clear HTTP/1.1" 415 1051
cURL komutunun doğru formatı nedir?
Bu benim Java tarafındaki PUT
kodum (GET ve DELETE'i test ettim ve çalışıyorlar):
@RequestMapping(method = RequestMethod.PUT)
public Configuration updateConfiguration(HttpServletResponse response, @RequestBody Configuration configuration) { //consider @Valid tag
configuration.setName("PUT worked");
//todo If error occurs response.sendError(HttpServletResponse.SC_NOT_FOUND);
return configuration;
}
İçerik türünüzü application/json olarak ayarlamanız gerekir. Ancak -d
, Spring'in tarafında kabul edilmeyen Content-Type application/x-www-form-urlencoded
gönderir.
curl man sayfasına]1 baktığımda, -H
kullanabileceğinizi düşünüyorum:
-H "Content-Type: application/json"
Tam örnek:
curl --header "Content-Type: application/json" \
--request POST \
--data '{"username":"xyz","password":"xyz"}' \
http://localhost:3000/api/login
(-H
--header
için, -d
ise --data
için kısaltmadır)
dbayrağı bir POST isteği anlamına geldiğinden,
-dkullanırsanız
-request POST` seçeneğinin isteğe bağlı olduğunu unutmayın.
Windows'ta işler biraz farklıdır. Yorum başlığına bakın.