如何通过curl发送REST XML POST请求?

时间:2011-08-10 22:08:45

标签: http rest curl jaxb http-post

基本上,我在Restlet中设置了一个项目,它使用JAXRS将资源映射到路径,并使用JAXB将XML序列化和反序列化为Java类型。我正在尝试发送POST请求以测试它是否有效,而且我遇到了一些麻烦。这是我的资源:

@Path("stream")
public class StreamResource {

    @POST
    @Consumes("text/xml")
    @Produces("text/xml")
    public Stream save(Stream value) {
        logger.debug("saving new stream...");
        return (Stream)this.streamPersistence.save(value);
    }
}

这是我的Stream课程:

@XmlRootElement(name="stream")
@XmlType(propOrder={"id", "streamName", "title", "description", fileSystemPath"})
public class Stream {

    private Long id;

    private String streamName;

    private String fileSystemPath;

    private String title;

    private String description;

    // getters/setters omitted for brevity
}

以下是我如何调用curl

curl -X POST -d '<stream><streamName>helloWorld.flv</streamName><title>Amazing Stuff, Dude!</title><description>This stream is awesome-cool.</description><fileSystemPath>/home/rfkrocktk/Desktop/helloWorld.flv</fileSystemPath></stream>' --header 'Content-Type:"text/xml"' http://localhost:8888/stream

以下是我从curl获得的错误:

The given resource variant is not supported.

...这是Restlet中的错误:

15:02:25.809 [Restlet-961410881] WARN  org.restlet.Component.Server - Error while parsing entity headers java.lang.IllegalArgumentException: Illegal token: "text
    at org.restlet.data.MediaType.normalizeToken(MediaType.java:647)
    at org.restlet.data.MediaType.normalizeType(MediaType.java:686)
    at org.restlet.data.MediaType.<init>(MediaType.java:795)
    at org.restlet.data.MediaType.<init>(MediaType.java:767)
    at org.restlet.engine.http.header.ContentTypeReader.createContentType(ContentTypeReader.java:84)
    at org.restlet.engine.http.header.ContentTypeReader.readValue(ContentTypeReader.java:112)
    at org.restlet.engine.http.header.ContentType.<init>(ContentType.java:99)
    at org.restlet.engine.http.header.HeaderUtils.extractEntityHeaders(HeaderUtils.java:664)
    at org.restlet.engine.http.connector.Connection.createInboundEntity(Connection.java:313)
    at org.restlet.engine.http.connector.ServerConnection.createRequest(ServerConnection.java:136)
    at org.restlet.engine.http.connector.ServerConnection.readMessage(ServerConnection.java:229)
    at org.restlet.engine.http.connector.Connection.readMessages(Connection.java:673)
    at org.restlet.engine.http.connector.Controller$2.run(Controller.java:95)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:679)

我在这里做错了什么?这看起来很简单,对吧?

2 个答案:

答案 0 :(得分:5)

删除text/xml周围的引号。

换句话说,你想要

curl -X POST -d '<stream><streamName>helloWorld.flv</streamName><title>Amazing Stuff, Dude!</title><description>This stream is awesome-cool.</description><fileSystemPath>/home/rfkrocktk/Desktop/helloWorld.flv</fileSystemPath></stream>' --header 'Content-Type: text/xml' http://localhost:8888/stream

答案 1 :(得分:0)

卷曲-v -H&#34;内容类型:application / xml&#34; -X POST --data-binary&#34; @ token.xml&#34; URL