This solution will work with the most complex types.
If anyone is interested in how to do this, I wrote an extension that should work with C # .Net Core 1.1 and Typescript 2.2.2 WebApi, which looks like this.
Remember to include these two imports, in which you also use it
import { URLSearchParams } from '@angular/http'; import 'rxjs/add/operator/map' export class QueryStringBuilder { static BuildParametersFromSearch<T>(obj: T): URLSearchParams { let params: URLSearchParams = new URLSearchParams(); if (obj == null) { return params; } QueryStringBuilder.PopulateSearchParams(params, '', obj); return params; } private static PopulateArray<T>(params: URLSearchParams, prefix: string, val: Array<T>) { for (let index in val) { let key = prefix + '[' + index + ']'; let value: any = val[index]; QueryStringBuilder.PopulateSearchParams(params, key, value); } } private static PopulateObject<T>(params: URLSearchParams, prefix: string, val: T) { const objectKeys = Object.keys(val) as Array<keyof T>; if (prefix) { prefix = prefix + '.'; } for (let objKey of objectKeys) { let value = val[objKey]; let key = prefix + objKey; QueryStringBuilder.PopulateSearchParams(params, key, value); } } private static PopulateSearchParams<T>(params: URLSearchParams, key: string, value: any) { if (value instanceof Array) { QueryStringBuilder.PopulateArray(params, key, value); } else if (value instanceof Date) { params.set(key, value.toISOString()); } else if (value instanceof Object) { QueryStringBuilder.PopulateObject(params, key, value); } else { params.set(key, value.toString()); } } }
This works for all the complex types that I have used so far.
johnny 5
source share