Al code - POST to rest API

terry823
Member Posts: 4
Hello,
I'm having trouble doing a post call to rest api. I've been able to successfully post using postman tool to confirm the API's working fine. I've also tried to use "Arend-Jan Kauffmann" github example kauffmann.nl/2017/07/18/al-web-service-examples/ but to it's not working in my case.
I need to post to get authentication token which would be sent over with subsequent requests until it expires. here is an example of what needs to happen:
url: https://testapi.com/gettoken
content-type:'applicaiton/application/x-www-form-urlencoded'
authorization:'Basic "base64secretkey"'
the above two are part of heard. Now I need to supply request content. In postman I add this to body raw:
scope=service&grant_type=client_credentials
In AL code I use httpclient:
gHttpClient.DefaultRequestHeaders.Add('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8');
gHttpClient.DefaultRequestHeaders.Add('authorization', 'Basic ' + lbase64key);
I have a httpcontent which I'm adding the content that needs to go in the request body then I assign this value to request content directly. I'm not sure if this is the right way of doing this part. I don't see an "Add content" method/property for request.
ghttpcontent:HttpContent;
greqMsg:HttpRequestMessage;
gresMsg:HttpResponseMessage;
ghttpcontent.Writefrom('scope=service&grant_type=client_credentials');
greqMsg.SetRequestUri(url);
greqMsg.Method := 'Post';
greqMsg.Content := gcontent;
ghttpclient.send(greqMsg,gresMsg);
I run into two issues here:
1: I get misused headers error with the content-type I defined on the httpclient.
2: When I remove this header for content-type from httpclient I get 400 code by checking the response code and result.
It would really help if someone can point me in the right direction. I've been at this for days now. By the way I've tested the get and it works.
Thanks!
I'm having trouble doing a post call to rest api. I've been able to successfully post using postman tool to confirm the API's working fine. I've also tried to use "Arend-Jan Kauffmann" github example kauffmann.nl/2017/07/18/al-web-service-examples/ but to it's not working in my case.
I need to post to get authentication token which would be sent over with subsequent requests until it expires. here is an example of what needs to happen:
url: https://testapi.com/gettoken
content-type:'applicaiton/application/x-www-form-urlencoded'
authorization:'Basic "base64secretkey"'
the above two are part of heard. Now I need to supply request content. In postman I add this to body raw:
scope=service&grant_type=client_credentials
In AL code I use httpclient:
gHttpClient.DefaultRequestHeaders.Add('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8');
gHttpClient.DefaultRequestHeaders.Add('authorization', 'Basic ' + lbase64key);
I have a httpcontent which I'm adding the content that needs to go in the request body then I assign this value to request content directly. I'm not sure if this is the right way of doing this part. I don't see an "Add content" method/property for request.
ghttpcontent:HttpContent;
greqMsg:HttpRequestMessage;
gresMsg:HttpResponseMessage;
ghttpcontent.Writefrom('scope=service&grant_type=client_credentials');
greqMsg.SetRequestUri(url);
greqMsg.Method := 'Post';
greqMsg.Content := gcontent;
ghttpclient.send(greqMsg,gresMsg);
I run into two issues here:
1: I get misused headers error with the content-type I defined on the httpclient.
2: When I remove this header for content-type from httpclient I get 400 code by checking the response code and result.
It would really help if someone can point me in the right direction. I've been at this for days now. By the way I've tested the get and it works.
Thanks!
0
Best Answer
Answers
-
Hi terry,
I am also trying to access the access token for my app in azure and I have done lots of experiments but didn't work, I am not able to get the authorisation code using GET request. Can you please let me know how can I do that.
I have failed trying HTTP Client, HTTP Request, AuthroizationCodeFlow, AuthenticationContext, Identity model and others (in separate attempts), It would be really helpful if you can help me.
Regards,
Chandra Prakash Soni0
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