2

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!

Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105
Drew
  • 710
  • 1
  • 15
  • 34

1 Answers1

0

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.

Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
  • Hi Robby, Thanks for the format but when I tried testing it, it is still giving me a java.lang.StringIndexOutOfBoundsException: String index out of range: -1. Is there a problem with the data format that is being posted? – Drew Aug 08 '14 at 06:11
  • @Drew Looking into that now. Are the `curl` errors gone, at least? – Robby Cornelissen Aug 08 '14 at 06:14
  • Yes Robby, they are gone. – Drew Aug 08 '14 at 06:15
  • @Drew I've updated my answer a couple of times, but it seems there are a number of issues. – Robby Cornelissen Aug 08 '14 at 06:31
  • Yes that's right the data type should be JSON, sorry forgot to mention it. I've tried using the format you gave, this time it's a different error javax.xml.stream.XMLStreamException: Unexpected end of json stream. Seems there is a problem with the input data on JSON format. Sorry about this I'm new to JSON and WSO2 products but this is what my superiors are requiring me to do. – Drew Aug 08 '14 at 06:53
  • Maybe try without the `data=` this time? Do you have a stacktrace? The JSON data in itself looks absolutely fine. – Robby Cornelissen Aug 08 '14 at 06:55
  • i did try without "data=" and got a more specific error "Input JSON message is not valid expected : _postuser but found : first_name" it seems I have to declare what object it is related to the table which is users. Do you have any idea on how to put it inside the input data? – Drew Aug 08 '14 at 07:02
  • You could try `{"_postuser":{ "first_name":"chris", ... }}` – Robby Cornelissen Aug 08 '14 at 07:04
  • Great news indeed! I've updated my answer to add the `_postuser` part. – Robby Cornelissen Aug 08 '14 at 07:09