HttpWebRequestMgt.GetResponse not getting response

DavidJs
Member Posts: 9
Hi! I'm trying to connect to a REST Api.
I'm using XMLport to send and receive the JSON and looks like it works fine until the response.
I thought that my variables may not be the ones that the function needs, so here are the ones im using:
HttpWebRequestMgt Codeunit Http Web Request Mgt.
HttpResponseHeader DotNet System.Net.HttpWebResponse.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
HttpStatusCodes DotNet System.Net.HttpStatusCode.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
There may be an easier way to do this. So i'm open to hear whatever opinions & tips you guys may have.
Thanks in advance
I'm using XMLport to send and receive the JSON and looks like it works fine until the response.
HttpWebRequestMgt.Initialize(URL); HttpWebRequestMgt.DisableUI; HttpWebRequestMgt.SetMethod('POST'); HttpWebRequestMgt.SetReturnType('application/json'); HttpWebRequestMgt.SetContentType('application/json'); HttpWebRequestMgt.AddBodyBlob(ReqTokenTempBlob); TempBlob.INIT; TempBlob.Blob.CREATEINSTREAM(InStr); IF NOT HttpWebRequestMgt.GetResponse(InStr, HttpStatusCodes ,HttpResponseHeader) THEN [...]
I thought that my variables may not be the ones that the function needs, so here are the ones im using:
HttpWebRequestMgt Codeunit Http Web Request Mgt.
HttpResponseHeader DotNet System.Net.HttpWebResponse.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
HttpStatusCodes DotNet System.Net.HttpStatusCode.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
There may be an easier way to do this. So i'm open to hear whatever opinions & tips you guys may have.
Thanks in advance
0
Answers
-
So, what response do you get?
What does the API documentation say? Do you need to send data with your request in some other way as as part of the URL?
I am not aware of the option to process JSON with an XML Port. When did this get added?
0 -
Can't help you with the codeunit unfortunately, never used it myself.
I always just used the variables and created my own codeunit to easily do API calls.
This is not an exact copy of it, but it might help you.local procedure DoAPIGet(URL: Text) var _HttpClient: HttpClient; _HttpHeaders: HttpHeaders; _JSONResponse: Text; _HttpResponseMessage: HttpResponseMessage; begin _HttpHeaders := _HttpClient.DefaultRequestHeaders(); _HttpHeaders.Clear(); _HttpHeaders.Add('Authorization', 'Bearer ' + GetAccessToken()); _HttpClient.Get(URL, _HttpResponseMessage); _HttpResponseMessage.Content.ReadAs(_JSONResponse); HandleResponse(_JSONResponse, _HttpResponseMessage.IsSuccessStatusCode); end;
0 -
So, what response do you get?
What does the API documentation say? Do you need to send data with your request in some other way as as part of the URL?
The documentation says that the first contact should be with user/pwd/licensekey in order to get the auth token. In fact, using postman (an app to test those connections) with the same info is working nicely.
The translation would be something as "the connection was forced to stop because of the remote host"I am not aware of the option to process JSON with an XML Port. When did this get added?Can't help you with the codeunit unfortunately, never used it myself.
I always just used the variables and created my own codeunit to easily do API calls.
This is not an exact copy of it, but it might help you.local procedure DoAPIGet(URL: Text) var _HttpClient: HttpClient; _HttpHeaders: HttpHeaders; _JSONResponse: Text; _HttpResponseMessage: HttpResponseMessage; begin _HttpHeaders := _HttpClient.DefaultRequestHeaders(); _HttpHeaders.Clear(); _HttpHeaders.Add('Authorization', 'Bearer ' + GetAccessToken()); _HttpClient.Get(URL, _HttpResponseMessage); _HttpResponseMessage.Content.ReadAs(_JSONResponse); HandleResponse(_JSONResponse, _HttpResponseMessage.IsSuccessStatusCode); end;
I'm going to give this a try! Thanks
edit: Btw @Resolus , HttpClient variable is ->
System.Net.Http.HttpClient.'System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' right?0 -
If you're still using C/AL, yes
The example above is from AL, but below should be the variables for C/ALHttpClient DotNet System.Net.Http.HttpClient.'System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' HttpResponseMessage DotNet System.Net.Http.HttpResponseMessage.'System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' HttpHeaders DotNet System.Net.Http.Headers.HttpHeaders.'System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
0 -
I am not aware of the option to process JSON with an XML Port. When did this get added?GUNNAR GESTSSON wrote:Then I convert the Xml to Json with a single function.
That explains it pretty much. XMLPort does not support JSON directly but XML can be convertetd to JSON and JSON to XML.
0 -
If you're still using C/AL, yes
The example above is from AL, but below should be the variables for C/ALHttpClient DotNet System.Net.Http.HttpClient.'System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' HttpResponseMessage DotNet System.Net.Http.HttpResponseMessage.'System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' HttpHeaders DotNet System.Net.Http.Headers.HttpHeaders.'System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
So i've tried to use some of the options I got here, and the:uri := uri.Uri(url); HttpClient := HttpClient.GetAsync(uri);
But i cannot get the response either...I am not aware of the option to process JSON with an XML Port. When did this get added?GUNNAR GESTSSON wrote:Then I convert the Xml to Json with a single function.
That explains it pretty much. XMLPort does not support JSON directly but XML can be convertetd to JSON and JSON to XML.
It's not implemented but a useful workaround, the thing is not that the conversion isn't working, is the connection with the API. The conversion works pretty fine at the moment.
And yes, i need to send user/pasword/acceskey to verify everything so thats why i'm trying to send the json too0 -
Hmm, Normally the GetAsync should return a HttpResponseMessage object, not a HttpClient object.
I've looked into our pre-AL code, and it seems you might only need to add .Result at the end to get the right object.HttpResponseMessage := HttpClient.GetAsync(_URI).Result; IF (NOT HttpResponseMessage.IsSuccessStatusCode) THEN EXIT; ResultString := HttpResponseMessage.Content.ReadAsStringAsync.Result;
0 -
Hmm, Normally the GetAsync should return a HttpResponseMessage object, not a HttpClient object.
I've looked into our pre-AL code, and it seems you might only need to add .Result at the end to get the right object.HttpResponseMessage := HttpClient.GetAsync(_URI).Result; IF (NOT HttpResponseMessage.IsSuccessStatusCode) THEN EXIT; ResultString := HttpResponseMessage.Content.ReadAsStringAsync.Result;
Thanks for checking that and telling me, I'm still having some threading errors.
I will try to fix those and reply with the result! Thanks in advance!
0
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