私はUbuntuを使っていて、インストールしましたカールSpring REST アプリケーションを cURL でテストしたいです。POST コードは Java 側で書きました。ただし、cURL でテストしたいです。JSON データを投稿しようとしています。サンプル データは次のようになります。
{"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}
私はこのコマンドを使用します:
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
次のエラーが返されます:
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
エラーの説明は次のとおりです:
要求エンティティが、要求されたメソッド () の要求されたリソースでサポートされていない形式であるため、サーバーはこの要求を拒否しました。
Tomcat ログ: "POST /ui/webapp/conf/clear HTTP/1.1" 415 1051
cURL コマンドの正しい形式は何ですか?
これは私の Java 側のPUT
コードです (GET と DELETE をテストしましたが、動作します)。
@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;
}
ベストアンサー1
コンテンツタイプをapplication/jsonに設定する必要があります。しかし-d
(または--data
) はデフォルトで Content-Type を送信しますapplication/x-www-form-urlencoded
が、これは Spring 側では受け入れられません。
を見てcurl マニュアルページ、使えると思います-H
(または--header
):
-H "Content-Type: application/json"
完全な例:
curl --header "Content-Type: application/json" \
--request POST \
--data '{"username":"xyz","password":"xyz"}' \
http://localhost:3000/api/login
(-H
は の略で--header
、-d
の略です--data
)
フラグは POST リクエストを意味するため、を使用する場合は はオプションで-request POST
あることに注意してください。-d
-d
Windows では状況が少し異なります。コメント スレッドを参照してください。