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

标签 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)

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

最佳答案

删除 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

关于http - 如何通过 curl 发送 REST XML POST 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7018489/

相关文章:

php - 如何连续运行 PHP 脚本?

java - HTTP 前端服务器如何与 Websphere 通信

REST 与 JSON-RPC?

linux - curl 从集群节点上的 docker 到主节点

php - Paypal API - CURRENCY_NOT_ALLOWED

java - 如何在 Angular 前端中从 REST-Backend 接收一个字符串

facebook-graph-api - 使用 graph api 隐藏帖子

.net - HTTP 最后更新

node.js - Node.js 请求 : HPE_INVALID_HEADER_TOKEN

http - 是什么导致我的 HTTP 服务器失败并返回 "exit status -1073741819"?