Send JSON in http Get with angular and Web Api

Requirement: Need to send complex in http get request using Angular and get the JSON in Web API

First prepare URLSearchParams object and add key value pairs.
Send the RequestOptions to Angular http service.

final url format will be
{{API-URL}}/api/folders/370/documents?filter={ 'startDate':'08/01/2005', 'endDate':'08/31/2005','currentPage': '2', 'pageLimit': '50'}

import {Http, Headers, RequestOptions, Response, ResponseContentType, URLSearchParams} from "@angular/http";
export class DocumentService
constructor(private http: Http)
loadDocuments(folderId: number, currentPage: number) : Observable<Response>
let headers = new Headers({
'Content-Type' : 'application/json',
'Accept': 'application/json'
let requestOptions = new RequestOptions({headers});
requestOptions.params = this.getFilterData(currentPage);
let path = "folders/"+folderId+"/documents";
return this.http.get(appConstants.apiUrl+ path, requestOptions );
private getFilterData(currentPage: number) : any
let params: URLSearchParams = new URLSearchParams();
let filterValue = JSON.stringify( {currentPage: currentPage, pageLimit: 50 });
params.set('filter', filterValue);
return params;
view raw service.ts hosted with ❤ by GitHub

On the API Side
Got the query parameter and deserialize to ViewModel
Your server side ViewModel matches your JSON object sent in the request.

var filterData = Request.GetQueryNameValuePairs();
if (filterData == null || !filterData.Any())
throw new ArgumentException("Invalid request: search parameters are missing.");
var viewModel = JsonConvert.DeserializeObjectfilterData.First().Value);

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s