TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Anton Nijus
NA
25
1.6k
How do i consume web API from console application ?
Mar 29 2018 3:57 AM
I am getting a response from the postman. But when I run my console application i cant catch the response data. Please someone guide how to do this? This is GPS data, providing API from one of our client, and i just want to consume the data and save the response from the API. I am passing vechile Numbers,start and end dates and getting the data in fields.
static
async Task RunAsync()
{
using
(var client =
new
HttpClient())
{
client.BaseAddress =
new
Uri(
"http://api.blabla.com/"
);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(
new
MediaTypeWithQualityHeaderValue(
"application/json"
));
try
{
// HTTP POST
var dat =
new
APIEntity()
{
unitCodes =
new
List<
int
>() {19215,19216,...}, /
/ Multiple vehicle numbers
start = DateTime.ParseExact(
"2018-03-12 01:00:00"
,
"yyyy-MM-dd HH:mm:ss"
,
null
),
// format : ISO 8601.
end = DateTime.ParseExact(
"2018-03-12 23:00:00"
,
"yyyy-MM-dd HH:mm:ss"
,
null
),
// format : ISO 8601.
fields =
new
List<
string
>()
{
"unitCode"
,
"timedate"
,
"latitude"
,
"longitude"
,
"ignition"
,
"velocity"
,
"positionerror"
,
"digitherm5"
,
"direction"
,
"distance"
}
// I should get the values of the fields and save it to my database. };
HttpResponseMessage postresponse = await client.PostAsJsonAsync(
"api/points"
, dat);
postresponse.EnsureSuccessStatusCode();
// postrespones code 200 ok.
if
(postresponse.IsSuccessStatusCode)
{
????????
// how to fetch the data?
}
}
catch
(HttpRequestException e)
{
Console.WriteLine(e.Message);
}
}
}
static
void
Main()
{
RunAsync().Wait();
}
My Model is:
public
class
APIEntity
{
public
List<
int
> unitCodes {
get
;
set
; }
public
DateTime start {
get
;
set
; }
public
DateTime end {
get
;
set
; }
public
List<
string
> fields {
get
;
set
; }
}
For your reference : Postman Request is :
{ "unitCodes": [ 19215, 19216, 19224 ], "start":"2018-03-20T09:00:00.000Z", "end":"2018-03-20T15:10:00.000Z", "fields": [ "unitCode", "timedate", "latitude", "longitude", "ignition", "velocity", "positionerror", "digitherm5", "direction", "distance" ] } and Respose is : { "status": "Success", "data": [ { "unitCode": 19215, "timedate": "2018-03-20 09:01:23", "latitude": 25.216296552475924, "longitude": 55.26109752059169, "ignition": false, "velocity": 0, "positionError": false, "seatCount": 0 }, { "unitCode": 19215, "timedate": "2018-03-20 09:11:23", "latitude": 25.216199149650755, "longitude": 55.261200652994816, "ignition": false, "velocity": 0.03, "positionError": false, "seatCount": 0 }, ...... ............... }
I have a doubt, Since its a post request, how should I receive the response from the colsole application? Should i really need to write GET request? but they are providing only the POST method ? Is this possible to achieve this?
Attachment:
Post-200ok.rar
Reply
Answers (
4
)
transfer file from ftp server to http server using C#
SHOWING LARGE RECORDS USING C# APPLICATION