Page 1 of 1

Please help, Olingo Insert Entity Issue (400) Bad Request

Posted: Sun May 17, 2015 11:32 pm
by macsir
Hi, All
Please help me on this this inserting operation, always get (400) Bad Request from line "req.execute()". Following is the original code and output.

public class OlingoCUD {
public static void main(String[] args) throws Exception {
String SERVICE_ROOT = "http://server:8001/api/v1/";
ODataClient client = ODataClientFactory.getClient();
String authStr = "username:pwd:NamespaceID";
String authEncoded = new String(Base64.encodeBase64(authStr.getBytes()));

ClientEntity newprocess = client.getObjectFactory().newEntity(new FullQualifiedName("ibm.tm1.api.v1.Process"));
newprocess.getProperties().add(client.getObjectFactory().newPrimitiveProperty("Name",client.getObjectFactory().newPrimitiveValueBuilder().buildString("MyProcess")));

URI ProcessURI = client.newURIBuilder(SERVICE_ROOT).appendEntitySetSegment("Processes").build();

ODataEntityCreateRequest<ClientEntity> req = client.getCUDRequestFactory().getEntityCreateRequest(ProcessURI, newprocess);
req.addCustomHeader("Authorization","CAMNamespace " + authEncoded);
req.setFormat(ODataFormat.APPLICATION_JSON);
ODataEntityCreateResponse<ClientEntity> res = req.execute();
System.out.println(res.getHeaderNames());

if (res.getStatusCode()==201) {
System.out.println("Successfully Created!");
}

}
}


Debug OutPut:
main] WARN org.apache.olingo.client.core.communication.header.ODataErrorResponseChecker - Error deserializing error response
java.lang.NullPointerException
at org.apache.olingo.commons.core.serialization.JsonODataErrorDeserializer.doDeserialize(JsonODataErrorDeserializer.java:46)
at org.apache.olingo.commons.core.serialization.JsonDeserializer.toError(JsonDeserializer.java:416)
at org.apache.olingo.client.core.serialization.ClientODataDeserializerImpl.toError(ClientODataDeserializerImpl.java:90)
at org.apache.olingo.client.core.serialization.ODataReaderImpl.readError(ODataReaderImpl.java:85)
at org.apache.olingo.client.core.communication.header.ODataErrorResponseChecker.checkResponse(ODataErrorResponseChecker.java:58)
at org.apache.olingo.client.core.communication.request.AbstractRequest.checkResponse(AbstractRequest.java:54)
at org.apache.olingo.client.core.communication.request.AbstractODataRequest.doExecute(AbstractODataRequest.java:316)
at org.apache.olingo.client.core.communication.request.cud.ODataEntityCreateRequestImpl.execute(ODataEntityCreateRequestImpl.java:88)
at org.apache.olingo.client.core.communication.request.cud.ODataEntityCreateRequestImpl.execute(ODataEntityCreateRequestImpl.java:47)
at Operation.OlingoCUD.main(OlingoCUD.java:62)
Exception in thread "main" org.apache.olingo.client.api.communication.ODataClientErrorException: (400) Bad Request [HTTP/1.1 400 Bad Request]
at org.apache.olingo.client.core.communication.header.ODataErrorResponseChecker.checkResponse(ODataErrorResponseChecker.java:74)
at org.apache.olingo.client.core.communication.request.AbstractRequest.checkResponse(AbstractRequest.java:54)
at org.apache.olingo.client.core.communication.request.AbstractODataRequest.doExecute(AbstractODataRequest.java:316)
at org.apache.olingo.client.core.communication.request.cud.ODataEntityCreateRequestImpl.execute(ODataEntityCreateRequestImpl.java:88)
at org.apache.olingo.client.core.communication.request.cud.ODataEntityCreateRequestImpl.execute(ODataEntityCreateRequestImpl.java:47)
at Operation.OlingoCUD.main(OlingoCUD.java:62)

Re: Please help, Olingo Insert Entity Issue (400) Bad Reques

Posted: Mon May 18, 2015 10:40 pm
by macsir
Anyone has ideas? :idea:

Re: Please help, Olingo Insert Entity Issue (400) Bad Reques

Posted: Tue May 19, 2015 1:05 pm
by dr.nybble
Try to "manually" POST your request through Chrome using the Advanced Rest Client plugin first to ensure you are forming the request correctly.
Trying it without setting any additional headers results in:

{
error: {
code: ""
message: "The content type specified is not supported. Please use JSON (application/json; charset=utf-8) as the content type."
}-
}


OK trying again with the right Content-Type and this body:

{
"Name": "TestProc"
}

Works -- I got 201 Created.

Try http://www.pocketsoap.com/tcpTrace/ to proxy your requests and see all of the traffic going back & forth (or enable wire logging).
Alternatively configure Eclipse to break on Null Pointer Exceptions and see what the response text is that is causing the null pointer exception.

Re: Please help, Olingo Insert Entity Issue (400) Bad Reques

Posted: Thu May 21, 2015 3:47 am
by macsir
dr.nybble wrote:Try to "manually" POST your request through Chrome using the Advanced Rest Client plugin first to ensure you are forming the request correctly.
Trying it without setting any additional headers results in:

{
error: {
code: ""
message: "The content type specified is not supported. Please use JSON (application/json; charset=utf-8) as the content type."
}-
}


OK trying again with the right Content-Type and this body:

{
"Name": "TestProc"
}

Works -- I got 201 Created.

Try http://www.pocketsoap.com/tcpTrace/ to proxy your requests and see all of the traffic going back & forth (or enable wire logging).
Alternatively configure Eclipse to break on Null Pointer Exceptions and see what the response text is that is causing the null pointer exception.
Thanks, Man. Your tool is helpful! I will give it a go!
But I have implemented the new codes successfully using HttpClient and Gson in java for create/delete/update objects on TM1 server! :D :D :D

Re: Please help, Olingo Insert Entity Issue (400) Bad Reques

Posted: Thu May 21, 2015 1:09 pm
by dr.nybble
Good news! We are using Apache HTTP Client and Jackson.