javacurlwso2wso2-data-services-server

curl errors: could not resolve host, java.lang.StringIndexOutOfBoundsException: String index out of range: -1


Sorry for posting this as this might be a simple problems to experts but to me as a beginner it is an important issue to resolve. I have created a WSO2 DSS service where it has a MYSQL table datasource named users(userid(auto_increment),first_name,last_name,password).

I created specific queries for GET, POST, PUT, DELETE and also resources to accommodate these request. When I started testing the service using cURL GET and DELETE, it was working and I was able to retrieve and delete data. However when I was trying to insert (POST) and update (PUT) data I'm getting multiple errors shown below:

curl: (6) Could not resolve host: first_name curl: (6) Could not
 resolve host: last_name curl: (6) Could not resolve host: password
 HTTP/1.1 500 Internal Server Error Access-Control-Allow-Headers:
 authorization,Access-Control-Allow-Origin,Content-Type Vary:
 Accept-Encoding Content-Type: text/html;charset=utf-8 Date: Fri, 08
 Aug 2014 05:05:43 GMT Server: WSO2-PassThrough-HTTP Transfer-Encoding:
 chunked

<html><head><title>Apache Tomcat/7.0.34 - Error
 report</title><style><!--H1
 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;}
 H2
 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;}
 H3
 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;}
 BODY
 {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;}
 B
 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P
 {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A
 {color : black;}A.name {color : black;}HR {color :
 #525D76;}--></style> </head><body><h1>HTTP Status 500 - String index out of range: 1</h1><HR size="1" noshade="noshade"><p><b>type</b>
 Exception report</p><p><b>message</b> <u>String index out of range:
 -1</u></p><p><b>description</b> <u>The server encountered an internal error that prevented t from fulfilling this
 request.</u></p><p><b>exception</b>
 <pre>java.lang.StringIndexOutOfBoundsException: String index out of
 range: -1
         java.lang.String.substring(String.java:1911)
         rg.apache.axis2.builder.XFormURLEncodedBuilder.extractParametersFromRequest(XFormURLEncodedBilder.java:174)
         rg.apache.axis2.builder.XFormURLEncodedBuilder.processDocument(XFormURLEncodedBuilder.java:12)
         rg.apache.axis2.transport.TransportUtils.createDocumentElement(TransportUtils.java:188)
         org.apache.axis2.transport.TransportUtils.createSOAPMessage(TransportUtils.java:146)
         org.apache.axis2.transport.http.util.RESTUtil.processXMLRequest(RESTUtil.java:65)
         org.apache.axis2.transport.http.AxisServlet$RestRequestProcessor.processXMLRequest(AxisServlet.java:826)
         org.apache.axis2.transport.http.AxisServlet.doPut(AxisServlet.java:317)
         javax.servlet.http.HttpServlet.service(HttpServlet.java:758)
         javax.servlet.http.HttpServlet.service(HttpServlet.java:848)
         org.eclipse.equinox.http.servlet.internal.ServletRegistration.service(ServletRegistration.java:61)
         org.eclipse.equinox.http.servlet.internal.ProxyServlet.processAlias(ProxyServlet.java:128)
         org.eclipse.equinox.http.servlet.internal.ProxyServlet.service(ProxyServlet.java:68)
         javax.servlet.http.HttpServlet.service(HttpServlet.java:848)
         org.wso2.carbon.tomcat.ext.servlet.DelegationServlet.service(DelegationServlet.java:68)
         org.wso2.carbon.tomcat.ext.filter.CharacterSetFilter.doFilter(CharacterSetFilter.java:61)

Here are the cURL commands that I used:

POST - curl -i -H "Authorization: Bearer f56ce778dee04274eba0eab3e9d7b324" -H "Accept: application/json" -X POST -d {"first_name":"chris","last_name":"paul","password":"cp3"} http://ip:port/SampleDSS/user/

PUT - curl -i -H "Authorization: Bearer f56ce778dee04274eba0eab3e9d7b324" -H "Accept: application/json" -X PUT -d {"userid":11,"first_name":"chris","last_name":"paul","password":"cp3"} http://ip:port/SampleDSS/user/

Can you please point out if there is any prob with the cURL command or the JSON format data that I'm using?

Any substantial help would be welcomed.

Thank you very much!


Solution

  • Quote your JSON data as follows:

    curl -i \
        -H "Authorization: Bearer f56ce778dee04274eba0eab3e9d7b324" \
        -H "Accept: application/json" \
        -H "Content-Type: application/json" \
        -X PUT \
        -d '{"_postuser":{"userid":11,"first_name":"chris","last_name":"paul","password":"cp3"}}' \
        http://ip:port/SampleDSS/user/
    

    It seems that your request is being processed as an XML request. You probably want to add a Content-Type header to indicate that you're sending JSON content.