Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Unanswered

Plugin Posting Api returning 400 Bad Request

(0) ShareShare
ReportReport
Posted on by 2
Hi , I developing an plugin where when trigger by an button, but i meet an issue which is my httpclient acting weird, when i using postman to test its working correct return 201 created etc, even i work at consoleapp for testing purpose ,its also showing me 201 status created.
but when i implement to my plugin it show 400 bad request .  
 
here my code for reference . appreciate for help me 
 
 
 private async Task InsertInvoiceToBusinessCentralAsync(string jsonContent, string bcODataEndpoint)
 {
     using (HttpClient client = new HttpClient())
     {
         try
         {
             // Get a new access token
             string accessToken = await GetAccessToken(client);
             // Use the new access token for the Authorization header
             client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(/Bearer/, accessToken);
             // Set the content type to application/json
             client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(/application/json/));
             // Send a POST request to the Business Central OData endpoint for inserting the invoice
             HttpResponseMessage response = await client.PostAsync(bcODataEndpoint, new StringContent(jsonContent, System.Text.Encoding.UTF8, /application/json/));
            
             if (!response.IsSuccessStatusCode)
             {
                 if (response.StatusCode == HttpStatusCode.BadRequest)
                 {
                     // Handle Bad Request error
                     string responseContent = await response.Content.ReadAsStringAsync();
                     
                     // Log responseContent for debugging
                     // throw new CustomException(/Bad Request: / + responseContent);
                 }
                 else if (response.StatusCode == HttpStatusCode.Unauthorized)
                 {
                     // Handle Unauthorized error
                     // throw a CustomException(/Unauthorized/);
                 }
                 else
                 {
                     // Handle other HTTP status codes or exceptions
                     // throw new CustomException(/Other error: / + response.ReasonPhrase);
                 }
             }
         }
         catch (HttpRequestException ex)
         {
             // Handle network-related errors
             // throw new CustomException(/Network error: / + ex.Message);
         }
     }
 }
  private async Task<string> GetAccessToken(HttpClient client)
  {
      var values = new Dictionary<string, string>
  {
      { /grant_type/, /refresh_token/ },
      { /refresh_token/, refreshToken },
      { /client_id/, clientId },
      { /client_secret/, clientSecret }
  };
      var content = new FormUrlEncodedContent(values);
      var response = await client.PostAsync(tokenEndpoint, content);
      var responseString = await response.Content.ReadAsStringAsync();
      if (!response.IsSuccessStatusCode)
      {
          throw new Exception(/Failed to refresh access token./);
      }
      var tokenResponse = JsonConvert.DeserializeObject<Dictionary<string, string>>(responseString);
      return tokenResponse[/access_token/];
  }
 var jsonData = new
 {
     No = invoiceNumber.ToString().Trim(),
     Sell_to_Customer_No = accountNumber.ToString().Trim(),
     Company_Bank_Account_Code = bankAccountName.ToString().Trim(),
     Shortcut_Dimension_1_Code = branchLabel.ToString().Trim(),
     Shortcut_Dimension_2_Code = insurancelabel.ToString().Trim(),
     Salesperson_Code67594 = salespersoncode.ToString().Trim(),
     Posting_Description = PostingDe.ToString()
 };
 string jsonContent = JsonConvert.SerializeObject(jsonData);
 InsertInvoiceToBusinessCentralAsync(jsonContent,bcODataEndpoint).GetAwaiter().GetResult();
 SyncInvoiceDetails(invoice, service);

 

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

Jainam Kothari – Community Spotlight

We are honored to recognize Jainam Kothari as our June 2025 Community…

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

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

Leaderboard > Customer experience | Sales, Customer Insights, CRM

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

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

#2
Vahid Ghafarpour Profile Picture

Vahid Ghafarpour 167 Super User 2025 Season 1

#3
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 138 Most Valuable Professional

Product updates

Dynamics 365 release plans