Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Got "entity [HTTP/1.1 400 Bad Request]" when insert enetity with apache olingo to Micosoft CRM 2016 online

Posted on by 10

Hello everyone,

I have a question about insert entity with Web API to Micosoft CRM online 2016.

package jzhao.crm.odata.example;

import java.io.IOException;

import org.apache.olingo.client.api.ODataClient;
import org.apache.olingo.client.api.communication.request.cud.ODataEntityCreateRequest;
import org.apache.olingo.client.api.communication.response.ODataEntityCreateResponse;
import org.apache.olingo.client.api.domain.ClientEntity;
import org.apache.olingo.client.api.http.HttpClientException;
import org.apache.olingo.client.api.uri.URIBuilder;
import org.apache.olingo.client.core.ODataClientFactory;
import org.apache.olingo.commons.api.edm.FullQualifiedName;
import org.apache.olingo.commons.api.http.HttpHeader;

public class ClientInputTest {

    ODataClient client = ODataClientFactory.getClient();

    // String serviceRoot = "https://{yours}.api.crm.dynamics.com/api/data/v8.1";
    String serviceRoot = "change to yours";

    String accessToken = "change to your token";

    public void testInsertEntity() {

        ClientEntity entity = client.getObjectFactory().newEntity(new FullQualifiedName("Microsoft.Dynamics.CRM.account"));
        entity.getProperties().add(client.getObjectFactory().newPrimitiveProperty("name",
                client.getObjectFactory().newPrimitiveValueBuilder().buildString("Test insert entity")));
        URIBuilder uriBuilder = client.newURIBuilder(serviceRoot).appendEntitySetSegment("accounts");

        ODataEntityCreateRequest<ClientEntity> entityCreateRequest = client.getCUDRequestFactory()
                .getEntityCreateRequest(uriBuilder.build(), entity);
        entityCreateRequest.addCustomHeader(HttpHeader.AUTHORIZATION, "Bearer " + accessToken);
        ODataEntityCreateResponse<ClientEntity> reponse = entityCreateRequest.execute();
        if (reponse.getStatusCode() == 201) {
            // created
        } else {
            throw new HttpClientException(reponse.getStatusMessage());
        }
    }

    public static void main(String[] args) throws IOException {
        ClientInputTest test = new ClientInputTest();
        test.testInsertEntity();
    }

I use think kind of java code to test insert entity. But it fails with:

Exception in thread "main" org.apache.olingo.client.api.communication.ODataClientErrorException: entity [HTTP/1.1 400 Bad Request]
	at org.apache.olingo.client.core.communication.header.ODataErrorResponseChecker.checkResponse(ODataErrorResponseChecker.java:75)
	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:310)
	at org.apache.olingo.client.core.communication.request.cud.ODataEntityCreateRequestImpl.execute(ODataEntityCreateRequestImpl.java:88)


Dependenty:

<project xmlns="maven.apache.org/.../4.0.0" xmlns:xsi="www.w3.org/.../XMLSchema-instance"
	xsi:schemaLocation="maven.apache.org/.../4.0.0 maven.apache.org/.../maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>jzhao.test</groupId>
	<artifactId>mscrm-odata-example</artifactId>
	<version>1.0-20170328</version>
	<packaging>jar</packaging>

	<name>mscrm-odata-example</name>
	<properties>
		<odata.version>4.3.0</odata.version>
	</properties>

	<dependencies>

		<dependency>
			<groupId>org.apache.olingo</groupId>
			<artifactId>odata-client-core</artifactId>
			<version>${odata.version}</version>
		</dependency>

		<dependency>
			<groupId>org.apache.olingo</groupId>
			<artifactId>odata-client-api</artifactId>
			<version>${odata.version}</version>
		</dependency>

		<dependency>
			<groupId>org.apache.olingo</groupId>
			<artifactId>odata-commons-api</artifactId>
			<version>${odata.version}</version>
		</dependency>
		<dependency>
			<groupId>org.apache.olingo</groupId>
			<artifactId>odata-commons-core</artifactId>
			<version>${odata.version}</version>
		</dependency>

	</dependencies>

</project>


Any one who get this kind of issue before ?

Many thanks!

*This post is locked for comments

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Got "entity [HTTP/1.1 400 Bad Request]" when insert enetity with apache olingo to Micosoft CRM 2016 online

    I was able to resolve this by disabling chunking on the client

    client.getConfiguration().setUseChuncked(false);




Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans