0

I'm trying to post an entity to a webservice, here the code:

 EmptyTagWrapper wrap = webResource.get(EmptyTagWrapper.class);

    client = Client.create();
    client.addFilter(new LoggingFilter(System.out));
    webResource = client.resource(MyWebservice.CREATETAGS.getUrl());

    ClientResponse clientResponse = webResource.post(ClientResponse.class, wrap);
    System.out.println(clientResponse);

If I try to do this manually it works:

http://www.mysite.de/api/tags/?xml=<?xml version="1.0" encoding="UTF-8"?><prestashop xmlns:xlink="http://www.w3.org/1999/xlink"><tag><name>Testtag</name><id_lang>1</id_lang></tag></prestashop>

but if I run my program, I get this error:

<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<errors>
<error>
<code><![CDATA[127]]></code>
<message><![CDATA[XML error : String could not be parsed as XML
XML length : 0
Original XML : ]]></message>
</error>
</errors>
</prestashop>

I think the reason is the linebreak that is appendet between the URL and the marshaled entity, here the sysout of the post-request:

    1 * Client out-bound request
1 > POST http://www.mysite.de/api/tags/?xml=
<?xml version="1.0" encoding="UTF-8"?><prestashop xmlns:xlink="http://www.w3.org/1999/xlink"><tag><name>Testtag</name><id_lang>1</id_lang></tag></prestashop>
1 * Client in-bound response
    1 < 500
    1 < Execution-Time: 0.006
    1 < Server: Apache
    1 < Access-Time: 1404134558
    1 < Connection: close
    1 < Vary: Host
    1 < PSWS-Version: 1.5.4.1
    1 < Content-Length: 282
    1 < Date: Mon, 30 Jun 2014 13:22:38 GMT
    1 < Content-Type: text/xml;charset=utf-8
    1 < X-Powered-By: PrestaShop Webservice
    1 < 

Any Ideas how to solve this?


Background

I've got different services and fields which are constructed depending on the entity and the service:

public enum PrestaWebservice {

    PRODUCTLINKS("Key1","products"),
    FULLPRODUCTLIST("Key1","products?display=["+PrestaProduct.FIELDS+"]"),
    CATEGORIESLIST("Key1","categories?display=["+PrestaCategory.FIELDS+"]"),
    CUSTOMERSLIST("Key2","customers?display=["+PrestaCustomer.FIELDS+"]"),
    TAGSLIST("Key2","tags?display=full"),
    EMPTYTAG("Key2","tags?schema=synopsis"),
    CREATETAGS("Key2","tags/?xml=");


    private final String SHOPADDRESS="http://www.mySite.de/api";
    private String key;
    private String url;

    private PrestaWebservice(String key, String url){
        this.key = key;
        this.url = url;
    }

    public String getKey(){
        return key;
    }

    public String getUrl(){
        return SHOPADDRESS+"/"+url;
    }


}
bdoughan
  • 147,609
  • 23
  • 300
  • 400
Java_Waldi
  • 924
  • 2
  • 12
  • 29

1 Answers1

0

In your question the URL is being constructed in your own code. You need to example in the following area why the carriage return is being added.

MyWebservice.CREATETAGS.getUrl()

Is there a reason you are send the XML as the value of a query parameter instead of in the body of the HTTP request?

bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • Well.. thank you for correction. I'm not sure if I understand your question, is my post-request "webResource.post(ClientResponse.class, wrap);" wrong? I've found it on several tutorials and thought this would be correct... Here for example http://stackoverflow.com/questions/2136119/using-the-jersey-client-to-do-a-post-operation ... What is the right way to send a jaxb-entity via post? – Java_Waldi Jul 01 '14 at 06:38