Calling Rest API

peterbbrainpeterbbrain Member Posts: 10
edited 2017-02-26 in NAV Three Tier
Failure to call a function on a payment provider API

I have created this three lines of code:

HttpWebRequest := HttpWebRequest.Create('https://sandbox.spryngpayments.com/v1/api_key');
HttpWebRequest.Method := 'GET';
HttpWebRequest.Headers.Add('X-APIKEY','DUMMYAPIKEY');
HttpWebResponse := HttpWebRequest.GetResponse;

Last Line raises error message:
A call to System.Net.HttpWebReqiuest.GetResponse failed with this message: An Existing connection was forcibly closed by the remote host.

Also when I use a valid APIKEY

When I try with postman, no problem whatsoever.




Best Answer

Answers

  • zohaibu95@hotmail.comzohaibu95@hotmail.com Member Posts: 223
    Use this
    bvbabccfiyaa.png
    jtiaal3zzueu.png
    CallRESTWebService(BaseUrl : Text;Method : Text;RestMethod : Text;VAR HttpContent : DotNet "System.Net.Http.HttpContent";VAR HttpResponseMessage : DotNet "System.Net.Http.HttpResponseMessage")
    
    HttpClient := HttpClient.HttpClient();
    HttpClient.BaseAddress := Uri.Uri(BaseUrl);
    
    CASE RestMethod OF
      'GET':    
        HttpResponseMessage := HttpClient.GetAsync(Method).Result;
      'POST':   
        HttpResponseMessage := HttpClient.PostAsync(Method,HttpContent).Result;
      'PUT':    
        HttpResponseMessage := HttpClient.PutAsync(Method,HttpContent).Result;
      'DELETE': 
        HttpResponseMessage := HttpClient.DeleteAsync(Method).Result;
    END;
    
    
    

    BaseUrl – the first part of the url that is being called, e.g.: http://www.webserviceaddress.com/

    Method – the resource on the web service, in other words, the second part of the url, e.g. somecoolapi/parameter?option=value

    RestMethod – one of the request verbs, either GET, POST, PUT or DELETE

    HttpContent – the content to be sent with the POST or PUT command

    HttpResponseMessage – the response message containing the status code and the response body. This parameter is ByVar because the calling code needs to handle the specific response itself. That’s not part of the generic pattern.
    Best Regards
    Zohaib Ahmed
    Dynamics NAV ERP Technical Consultant.

    please like / agree / verify my answer, if it was helpful for you. thanks.
  • peterbbrainpeterbbrain Member Posts: 10
    Thanks very much for your reply. Zohaib Ahmed

    Unfortunately this code gives exactly the same result. :'(
    Did you try it yourself with the API I want to use ('https://sandbox.spryngpayments.com/v1/api_key')
    them I am very curious for the actual code you are using.

    Any other suggestions?

  • peterbbrainpeterbbrain Member Posts: 10
    Thanks very much for this answer.

    I did manage to solve it earlier today with exact that line of code. o:)

    In the mean time discovered a lot about how to use fiddler in combination with NAV in order to get to the root cause of this problem.

    It was the SSL TSL12 and yes a payment provider.

    Cheers!
  • zohaibu95@hotmail.comzohaibu95@hotmail.com Member Posts: 223
    Thanks very much for this answer.

    I did manage to solve it earlier today with exact that line of code. o:)

    In the mean time discovered a lot about how to use fiddler in combination with NAV in order to get to the root cause of this problem.

    It was the SSL TSL12 and yes a payment provider.

    Cheers!

    Please accept my answer if it helps you.
    Best Regards
    Zohaib Ahmed
    Dynamics NAV ERP Technical Consultant.

    please like / agree / verify my answer, if it was helpful for you. thanks.
  • jwilderjwilder Member Posts: 263
    I cannot get Fiddler to work with NAV using similar code as you have above. How did you get Fiddler to work, there must be a trick to it?
  • AdamDwyerAdamDwyer Member Posts: 3
    edited 2018-07-26

    Fiddler is a nice tool for sniffing network traffic.
    It can be downloaded here: http://www.telerik.com/fiddler.
    When I needed to sniff my network traffic (NAV Consume Webservice Black-Belt ) I discovered that Fiddler “out-of-the-box” does not show the Network packages from NAV.
    This is cause NAV instance is running as a service and Fiddler only looks at processes within your current session.
    If you need to sniff the network packages from NAV as well please follow these steps:
    This is done like this:
    1. Go to : C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config
    2. locate the file machine.config.
    3. open the file (first create a backup)
    4. locate the section <system.net>  (or create this section at the bottom , within configuration tag).
    Update the section to:
    <!-- The following section is to force use of Fiddler for all
    applications, including those running in service accounts -->
    <system.net>
    <defaultProxy
    enabled = "true"
    useDefaultCredentials = "true">
    <proxy autoDetect="false" bypassonlocal="false"
    proxyaddress="http://127.0.0.1:8888&quot;
    usesystemdefault="false" />
    </defaultProxy>
    </system.net>
    5. Restart NAV Service tier.
    (do not forget to remove this section (use the backup from step 3) and restart NAV after sniffing.
  • AdamDwyerAdamDwyer Member Posts: 3
    3p6d39qjbp9d.png
Sign In or Register to comment.