Calling Rest API

peterbbrainpeterbbrain Posts: 10Member
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

  • 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 Posts: 10Member
    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 Posts: 10Member
    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!
  • 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 Posts: 247Member
    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?
Sign In or Register to comment.