httpclient: basic authentication

gbierkens
Member Posts: 49
I'm experimenting with integrating Dynamics NAV with a REST API.
I have created an API with basic authentication.
Now I would like NAV (2016) to send http requests with basic authentication.
I'm fairly new to dotnet interop so forgive me if I ask a stupid question
I Use the following code to send the post request
When I disable authentication in my webservice this works like a charm.
I tried the following this to add authentication to this request.
1. networkcredentials
this doesn't seem to do anyting.
2. default request headers
As you can probably see these are just trials which I distilled from several stack overflow posts.
But none of them are working. Can someone point me in the right direction?
I have created an API with basic authentication.
Now I would like NAV (2016) to send http requests with basic authentication.
I'm fairly new to dotnet interop so forgive me if I ask a stupid question

I Use the following code to send the post request
Method := 'POST'; HttpStringContent := json string HttpClient := HttpClient.HttpClient; HttpClient.BaseAddress := Uri.Uri(BaseUrl); HttpResponseMessage := HttpClient.PostAsync(Method,HttpStringContent).Result;
When I disable authentication in my webservice this works like a charm.
I tried the following this to add authentication to this request.
1. networkcredentials
NetworkCredential := NetworkCredential.NetworkCredential('abc','test'); HttpClientHandler := HttpClientHandler.HttpClientHandler; HttpClientHandler.Credentials := NetworkCredential; HttpClient := HttpClient.HttpClient(HttpClientHandler );
this doesn't seem to do anyting.
2. default request headers
HttpClient := HttpClient.HttpClient(); HttpClient.BaseAddress := Uri.Uri(BaseUrl); HttpClient.DefaultRequestHeaders.Add('Authorisation','Basic'); HttpClient.DefaultRequestHeaders.Add('PHP_AUTH_USER','abc');
As you can probably see these are just trials which I distilled from several stack overflow posts.
But none of them are working. Can someone point me in the right direction?
BriteBlue Solutions BV | briteblue.nl
0
Answers
-
I think the choice of HttpClient might be wrong.
This works for me (I use Fiddler to record what http headers are sent):HttpWebRequest := HttpWebRequest.Create('url'); HttpWebRequest.Credentials := Credential.NetworkCredential('myusername', 'mypassword'); HttpWebRequest.Method := 'POST';
Types:Name DataType Subtype Length HttpWebRequest DotNet System.Net.HttpWebRequest.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' Credential DotNet System.Net.NetworkCredential.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
You can find additional inspiration in codeunit 1297 in Dynamics NAV 2016.
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