Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested answer

return=representation error Dynamics WEB API

(0) ShareShare
ReportReport
Posted on by 5

I am trying to get response with return=representation but its saying bad header can anyone help on this

1.How to get errors in return=representation
2.How to add header and retrieve GUID for Creation on record i tried with following code

Dim URL As String = String.Empty
        Dim response As HttpResponseMessage
        Dim mailMessage As New StringBuilder
        Try
            System.Net.ServicePointManager.Expect100Continue = True
            System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12
            authContext = New AuthenticationContext(authority, False)
            clientCredential = New ClientCredential(clientId, appKey)
            Dim result As AuthenticationResult = Nothing
            result = authContext.AcquireToken(serviceUrl, clientCredential)
            httpClient.DefaultRequestHeaders.Authorization = New AuthenticationHeaderValue("Bearer", result.AccessToken)
            httpClient.DefaultRequestHeaders.Add("Prefer", "odata.includeannotations=OData.Community.Display.V1.FormattedValue")
            httpClient.DefaultRequestHeaders.Add("Prefer", "return=representation ")

            URL = "https"//test.dynamics.com" & "/api/data/v9.0/accounts?$select=name" 'Giving dummy url (https"//test.dynamics.com )for security reasons in question change your url 
            Dim createrequest1 As HttpRequestMessage = New HttpRequestMessage(HttpMethod.Post, URL)
            createrequest1.Content = New StringContent(InputJson, Encoding.UTF8, "application/json")
            response = httpClient.SendAsync(createrequest1).Result


            Dim contact1Uri As String = ""
            If response.StatusCode = System.Net.HttpStatusCode.NoContent Then
                contact1Uri = response.Headers.GetValues("OData-EntityId").FirstOrDefault()
            End If
            If (ConfigurationManager.AppSettings("IsEmail").ToString().ToUpper() = "ON".ToUpper() Or String.IsNullOrEmpty(contact1Uri)) Then
                Dim strEmailSubject As String = "FAILED: Error in Dynamics post method"
                mailMessage.Append("Operation attempted: Error in Dynamics post method" & vbCrLf & vbCrLf)
                mailMessage.Append("Error date: " & DateTime.Now & vbCrLf & vbCrLf)
                mailMessage.Append("File: " & HttpContext.Current.Request.FilePath & vbCrLf & vbCrLf)
                mailMessage.Append("Call: " & Reflection.MethodBase.GetCurrentMethod().Name & vbCrLf & vbCrLf)
                mailMessage.Append("General error: " & "Post URL :"   URL   "
Json Input :" InputJson "
Json Response :" response.ReasonPhrase.ToString() "" response.Content.ToString() "") Utilities.SendEmail(HttpContext.Current.Application.Item("ErrorEmail_From").ToString, HttpContext.Current.Application.Item("ErrorEmail_High"), String.Empty, String.Empty, strEmailSubject, mailMessage.ToString, False, Net.Mail.MailPriority.Normal) End If Return contact1Uri

  • Suggested answer
    Bipin D365 Profile Picture
    28,981 Moderator on at
    RE: return=representation error Dynamics WEB API

    Hi,

    Why do you need ?$select=name in URl as this is post request, no need to add this.

    Please remove this and try.

    See below link for sample cod to perform CRUD operation using WEB API C#.

    https://github.com/jlattimer/CrmWebApiCSharp/blob/master/CrmWebApiCSharp/Program.cs

    If found helpful, Please mark my answer verified.

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

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Daivat Vartak (v-9davar) Profile Picture

Daivat Vartak (v-9d... 225 Super User 2025 Season 1

#2
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 106 Most Valuable Professional

#3
Eugen Podkorytov Profile Picture

Eugen Podkorytov 95

Overall leaderboard

Product updates

Dynamics 365 release plans