web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

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

I have the same question (0)
  • Suggested answer
    Bipin D365 Profile Picture
    28,983 Moderator on at

    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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 70 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 33 Most Valuable Professional

#3
Daniyal Khaleel Profile Picture

Daniyal Khaleel 32 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans