HttpWebRequestMgt.GetResponse not getting response

DavidJsDavidJs 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.
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

Answers

  • vaprogvaprog GermanyMember Posts: 1,040
    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?
  • ResolusResolus Member Posts: 40
    edited 2022-10-21
    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;
    
  • DavidJsDavidJs Member Posts: 9
    edited 2022-10-21
    vaprog wrote: »
    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"
    iudkdckbapyf.png
    vaprog wrote: »
    I am not aware of the option to process JSON with an XML Port. When did this get added?
    im following an old post from Gunnar's blog and adapting it to my needs. So my guess is that its a few years old, at very least 6!



    Resolus wrote: »
    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?
  • ResolusResolus Member Posts: 40
    If you're still using C/AL, yes
    The example above is from AL, but below should be the variables for C/AL
    HttpClient	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'	
    
  • vaprogvaprog GermanyMember Posts: 1,040
    vaprog wrote: »
    I am not aware of the option to process JSON with an XML Port. When did this get added?
    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.

  • DavidJsDavidJs Member Posts: 9
    Resolus wrote: »
    If you're still using C/AL, yes
    The example above is from AL, but below should be the variables for C/AL
    HttpClient	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'	
    
    I've been trying to apply this, but at least in nav 17 httpClient does not have a GET function,
    ukn8vuj2d7z6.png

    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...

    vaprog wrote: »
    vaprog wrote: »
    I am not aware of the option to process JSON with an XML Port. When did this get added?
    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 too
  • ResolusResolus Member Posts: 40
    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;
    
  • DavidJsDavidJs Member Posts: 9
    Resolus wrote: »
    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!

Sign In or Register to comment.