cancel
Showing results for 
Search instead for 
Did you mean: 

Problems with Patch in C# Console App - Don't Work - Error 500

MikeRojas
Explorer
342

Hello community, I hope you can help me, we are developing a console application with C#.

When we test via POSTMAN, everything works and updates the records, but when we do it via C# it doesn't work.

We share with you 2 approaches with which we test in C3 and both approaches fail.

Our code :

from this repo

https://github.com/adrian-scan4sap/code-queries-sl/blob/main/code-queries-sl/Program.cs

public static async Task TestPatch(){ 
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
                string baseUrl = "https://1.10.80.20:40000/b1s/v1/";
                ServicePointManager.ServerCertificateValidationCallback = (sender, cert, chain, sslPolicyErrors) => true;
                string businessPartnerUrl = $"{baseUrl}/BusinessPartners('USA140FGH82500')";

                // Create a JSON payload for the new Business Partner
                JObject businessPartnerData = new JObject
            {
                { "CardCode", "USA140FGH82500" }, // Provide a unique Business Partner code
                {
                    "ContactEmployees", new JArray
                    {
                        new JObject
                        {
                            { "Name", "Example Name 1..." },
                        }
                    }
                }
            };
                try
                {
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(businessPartnerUrl);
                    request.Method = "PATCH";
                    request.ContentType = "application/json";
                    request.Accept = "application/json";
                    request.Headers.Add("Cookie", $"B1SESSION={'7545-8455-SA-TOKEN'}");
                    using (var streamWriter = new StreamWriter(request.GetRequestStream()))
                    {
                        streamWriter.Write(businessPartnerData.ToString());
                        streamWriter.Flush();
                        streamWriter.Close();
                    }
                    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                    {
                        if (response.StatusCode == HttpStatusCode.NoContent)
                        {
                            Console.WriteLine("Business Partner Contact updated successfully.");
                        }
                        else
                        {
                            Console.WriteLine("Failed to update Business Partner Contact. Status code: " + response.StatusCode);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("An error occurred while updating the Business Partner Contact: " + ex.Message);
                }

Result :  Error 500 !

 

please, can someone give us some help, some link, some repository, some code?

MikeRojas
Explorer
mwalter_SUND
Explorer

Hello MikeRojas,

it looks like you are triing to create a new BP. In this case you need to use post and not patch:

mwalter_SUND_0-1723452053790.png

 

View Entire Topic
EunSeok_Bang
Product and Topic Expert
Product and Topic Expert

Hello, 

Here are some suspictions.

 

1. Make sure the Service layer port is 40000. the port 40000 is typically used for SLD service. the default Service Layer port is 50000.

string baseUrl = "https://1.10.80.20:40000/b1s/v1/";

2. The variable for B1 session id "7545-8455-SA-TOKEN" looks strange. make sure if you used the correct variable to hold the the session id through login.

request.Headers.Add("Cookie", $"B1SESSION={'7545-8455-SA-TOKEN'}");

 3. As a general tip, it would be helpful to test with a simpler JSON format, for example, GET BP to isolate the issue from JSON format.

 

Regards
EunSeok

MikeRojas
Explorer
Hi @EunSeok_Bang If the port we are using is 50000, we only put 40 for example and the token was also an example. We decided to test with PHP, NodeJS and Python and the PATCH worked perfectly in all of them, but the PATCH did not work in C#.
EunSeok_Bang
Product and Topic Expert
Product and Topic Expert
Hi Mike, does it mean you succeeded with other methods like GET and POST, and only not successful with PATCH?
EunSeok_Bang
Product and Topic Expert
Product and Topic Expert
It would be also worth trying to specify TLS version e.g. 1.2 if you use B1 10.0, and try /b1s/v2 to be compatiable with OData V4 because otherwise I don't see an issue.
MikeRojas
Explorer
hi @EunSeok_Bang , only with PATCH