PATCH method

dreez
Member Posts: 73
Hi!
I have recently encountered a problem with PATCH call from C/AL.
I have already tried a solution for AL, but it didn't work, here's my code:
When I try to execute that, I am getting the following error:
A call to System.Net.Http.HttpRequestMessage.set_Method failed with this message: The type of one or more arguments does not match the method's parameter type.
Any ideas?
Thanks in advance.
I have recently encountered a problem with PATCH call from C/AL.
I have already tried a solution for AL, but it didn't work, here's my code:
CASE RestMethod OF 'PATCH': BEGIN String := 'PATCH' //it's a DotNet String variable HttpRequestMessage.Content(HttpContent); //HttpContent contains JSON object which I sent HttpRequestMessage.Method(String); //I've tried to use ('PATCH') instead of String, but it requires DotNet variable HttpResponseMessage := HttpClient.SendAsync(HttpRequestMessage); END; END;
When I try to execute that, I am getting the following error:
A call to System.Net.Http.HttpRequestMessage.set_Method failed with this message: The type of one or more arguments does not match the method's parameter type.
Any ideas?
Thanks in advance.
0
Best Answer
-
Unfortunately that didn't help.A call to System.Uri failed with this message: Invalid URI: The format of the URI could not be determined.
Is there any possibility to lookup how is, for example HttpClient.PutAsync method built?
I feel like I'm giving up, and maybe I could just use this PATCH method with REST Sharp library, that you have recommended..
Oh, looks like System.Uri needs absolute path, akaMethodUri := MethodUri.Uri(BaseUrl + Method);
And, naturally, make sure that either your BaseUrl ends with, or Method begins, with a slash1
Answers
-
HttpRequestMessage.Method requires a parameter (if setting a value) of type "HttpMethod", not "String". Once you have defined your variable (preferably with name "HttpMethod", your problematic line should be:
HttpRequestMessage.Method(HttpMethod.Patch);
0 -
HttpRequestMessage.Method requires a parameter (if setting a value) of type "HttpMethod", not "String". Once you have defined your variable (preferably with name "HttpMethod", your problematic line should be:
HttpRequestMessage.Method(HttpMethod.Patch);
Thank you for your answer, but unfortunately I've already tried that.
There is no such method as Patch - and that's the problem. There are methods like Get, Put and Post.
My code is inspired by this topic (hopefuly it's not a problem to link something from outside of this forum):
Patch_method_AL
0 -
Ah, I see. You're using the HttpRequestMessage (and HttpMethod) from the "System.Net.Http" namespace (in which HttpMethod does not have PATCH method), while I checked the "Windows.Web.Http".
You should try then (should work in theory):HttpMethod := HttpMethod.HttpMethod('PATCH'); HttpRequestMessage.Method(HttpMethod);
Alternatively, you could go with RestSharp library.1 -
Thanks, I feel like it's really close solution, because now I am getting the following error:A call to System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode failed with this message: Response status code does not indicate success: 302 (Found).
I think, that I know what's the reason, but I don't know how to fix the problem.
I will show you my entire code, so I can explain:LOCAL CallRESTWebService() HttpClient := HttpClient.HttpClient; HttpClient.BaseAddress := Uri.Uri(BaseUrl); IF AuthorizationHeader <> '' THEN HttpClient.DefaultRequestHeaders.Add('Authorization',AuthorizationHeader); CASE RestMethod OF 'GET': CurrHttpResponseMessage := HttpClient.GetAsync(Method).Result; 'POST': CurrHttpResponseMessage := HttpClient.PostAsync(Method,HttpContent).Result; 'PUT': CurrHttpResponseMessage := HttpClient.PutAsync(Method,HttpContent).Result; 'DELETE': CurrHttpResponseMessage := HttpClient.DeleteAsync(Method).Result; 'PATCH': BEGIN HttpRequestMessage := HttpRequestMessage.HttpRequestMessage; HttpMethod := HttpMethod.HttpMethod('PATCH'); HttpRequestMessage.Content(HttpContent); HttpRequestMessage.Method(HttpMethod); CurrHttpResponseMessage := HttpClient.SendAsync(HttpRequestMessage).Result; END; END; CurrHttpResponseMessage.EnsureSuccessStatusCode;
And maybe a little explanation here, RestMethod, as you can see is GET, POST, PUT, DELETE or PATCH. Also there is a Method (query) that I can use with API, for example 'api/employees' and I can combine that with different queries.
As you can see, HttpClient.Get/Post/Put/Delete methods takes this Method as a parameter, so now I don't know where to put this Method variable, so it could work with newly defined PATCH case.
I have tried something like:HttpMethod := HttpMethod.HttpMethod('PATCH ' + Method);
but it doesn't work and results with following error:A call to System.Net.Http.HttpMethod failed with this message: The format of the HTTP method is invalid.
Do you have any idea how to fix that?0 -
You should add the "Method" as part of your HttpRequestMessage.RequestUri property, which states that:If the request Uri is a relative Uri, it will be combined with the BaseAddress [of the HttpClient]
HttpRequestMessage.RequestUri(Method);
Sorry I can't test these things, don't have a proper platform with PATCH methods1 -
Sorry I can't test these things, don't have a proper platform with PATCH methodsSo you'd have a line
HttpRequestMessage.RequestUri(Method);
The thing is, that the method RequestUri requires a DotNet parameter, while my Method is a text.
I've just tried to do that the same way as before, so I added two lines:HttpMethod2 := HttpMethod2.HttpMethod(Method); HttpRequestMessage.RequestUri(HttpMethod2);
But it doesn't seem to work and results with an error:A call to System.Net.Http.HttpMethod failed with this message: The format of the HTTP method is invalid.0 -
Alright, how about:
<..> 'PATCH': BEGIN HttpRequestMessage := HttpRequestMessage.HttpRequestMessage; HttpMethod := HttpMethod.HttpMethod('PATCH'); HttpRequestMessage.Content(HttpContent); HttpRequestMessage.Method(HttpMethod); MethodUri := MethodUri.Uri(Method); HttpRequestMessage.RequestUri(MethodUri); CurrHttpResponseMessage := HttpClient.SendAsync(HttpRequestMessage).Result; END; <..>
Where:Name DataType Subtype Length MethodUri DotNet System.Uri.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
1 -
Unfortunately that didn't help.A call to System.Uri failed with this message: Invalid URI: The format of the URI could not be determined.
Is there any possibility to lookup how is, for example HttpClient.PutAsync method built?
I feel like I'm giving up, and maybe I could just use this PATCH method with REST Sharp library, that you have recommended..0 -
Unfortunately that didn't help.A call to System.Uri failed with this message: Invalid URI: The format of the URI could not be determined.
Is there any possibility to lookup how is, for example HttpClient.PutAsync method built?
I feel like I'm giving up, and maybe I could just use this PATCH method with REST Sharp library, that you have recommended..
Oh, looks like System.Uri needs absolute path, akaMethodUri := MethodUri.Uri(BaseUrl + Method);
And, naturally, make sure that either your BaseUrl ends with, or Method begins, with a slash1
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions