diff --git a/tns-core-modules/http/http-request.android.ts b/tns-core-modules/http/http-request.android.ts index 66e74f289..e85bc5cda 100644 --- a/tns-core-modules/http/http-request.android.ts +++ b/tns-core-modules/http/http-request.android.ts @@ -66,7 +66,7 @@ function onRequestComplete(requestId: number, result: org.nativescript.widgets.A var pair: org.nativescript.widgets.Async.Http.KeyValuePair; for (i = 0; i < length; i++) { pair = jHeaders.get(i); - + (http).addHeader(headers, pair.key, pair.value); } } @@ -74,16 +74,28 @@ function onRequestComplete(requestId: number, result: org.nativescript.widgets.A callbacks.resolveCallback({ content: { raw: result.raw, - toString: () => { - if (types.isString(result.responseAsString)) { - return result.responseAsString; + toString: (encoding?: http.HttpResponseEncoding) => { + let str: string; + if (encoding) { + str = decodeResponse(result.raw, encoding); + } else { + str = result.responseAsString; + } + if (types.isString(str)) { + return str; } else { throw new Error("Response content may not be converted to string"); } }, - toJSON: () => { + toJSON: (encoding?: http.HttpResponseEncoding) => { ensureUtils(); - return utils.parseJSON(result.responseAsString); + let str: string; + if (encoding) { + str = decodeResponse(result.raw, encoding); + } else { + str = result.responseAsString; + } + return utils.parseJSON(str); }, toImage: () => { ensureImageSource(); @@ -195,3 +207,11 @@ export function request(options: http.HttpRequestOptions): Promise { (http).addHeader(headers, key, value); }); } - + if (debugRequest) { debugRequest.mimeType = response.MIMEType; debugRequest.data = data; @@ -89,9 +89,9 @@ export function request(options: http.HttpRequestOptions): Promise { return NSDataToString(data); }, - toJSON: () => { - return utils.parseJSON(NSDataToString(data)); + toString: (encoding?: http.HttpResponseEncoding) => { return NSDataToString(data, encoding); }, + toJSON: (encoding?: http.HttpResponseEncoding) => { + return utils.parseJSON(NSDataToString(data, encoding)); }, toImage: () => { ensureImageSource(); @@ -125,7 +125,7 @@ export function request(options: http.HttpRequestOptions): Promise + /** + * Downloads the content from the specified URL as a string. + * @param url The URL to request from. + */ + export function getString(url: string): Promise - /** - * Downloads the content from the specified URL as a string. - * @param options An object that specifies various request options. - */ - export function getString(options: HttpRequestOptions): Promise + /** + * Downloads the content from the specified URL as a string. + * @param options An object that specifies various request options. + */ + export function getString(options: HttpRequestOptions): Promise - /** - * Downloads the content from the specified URL as a string and returns its JSON.parse representation. - * @param url The URL to request from. - */ - export function getJSON(url: string): Promise + /** + * Downloads the content from the specified URL as a string and returns its JSON.parse representation. + * @param url The URL to request from. + */ + export function getJSON(url: string): Promise - /** - * Downloads the content from the specified URL as a string and returns its JSON.parse representation. - * @param options An object that specifies various request options. - */ - export function getJSON(options: HttpRequestOptions): Promise + /** + * Downloads the content from the specified URL as a string and returns its JSON.parse representation. + * @param options An object that specifies various request options. + */ + export function getJSON(options: HttpRequestOptions): Promise - /** - * Downloads the content from the specified URL and attempts to decode it as an image. - * @param url The URL to request from. - */ - export function getImage(url: string): Promise + /** + * Downloads the content from the specified URL and attempts to decode it as an image. + * @param url The URL to request from. + */ + export function getImage(url: string): Promise - /** - * Downloads the content from the specified URL and attempts to decode it as an image. - * @param options An object that specifies various request options. - */ - export function getImage(options: HttpRequestOptions): Promise + /** + * Downloads the content from the specified URL and attempts to decode it as an image. + * @param options An object that specifies various request options. + */ + export function getImage(options: HttpRequestOptions): Promise + + /** + * Downloads the content from the specified URL and attempts to save it as file. + * @param url The URL to request from. + * @param destinationFilePath Optional. The downloaded file path. + */ + export function getFile(url: string, destinationFilePath?: string): Promise + + /** + * Downloads the content from the specified URL and attempts to save it as file. + * @param options An object that specifies various request options. + * @param destinationFilePath Optional. The downloaded file path. + */ + export function getFile(options: HttpRequestOptions, destinationFilePath?: string): Promise + + /** + * Makes a generic http request using the provided options and returns a HttpResponse Object. + * @param options An object that specifies various request options. + */ + export function request(options: HttpRequestOptions): Promise; + + /** + * Provides options for the http requests. + */ + export interface HttpRequestOptions { + /** + * Gets or sets the request url. + */ + url: string; /** - * Downloads the content from the specified URL and attempts to save it as file. - * @param url The URL to request from. - * @param destinationFilePath Optional. The downloaded file path. + * Gets or sets the request method. */ - export function getFile(url: string, destinationFilePath?: string): Promise + method: string; /** - * Downloads the content from the specified URL and attempts to save it as file. - * @param options An object that specifies various request options. - * @param destinationFilePath Optional. The downloaded file path. + * Gets or sets the request headers in JSON format. */ - export function getFile(options: HttpRequestOptions, destinationFilePath?: string): Promise + headers?: any; - /** - * Makes a generic http request using the provided options and returns a HttpResponse Object. - * @param options An object that specifies various request options. - */ - export function request(options: HttpRequestOptions): Promise; + /** + * Gets or sets the request body. + */ + content?: string | FormData; - /** - * Provides options for the http requests. - */ - export interface HttpRequestOptions { - /** - * Gets or sets the request url. - */ - url: string; + /** + * Gets or sets the request timeout in milliseconds. + */ + timeout?: number; + } - /** - * Gets or sets the request method. - */ - method: string; + /** + * Encapsulates HTTP-response information from an HTTP-request. + */ + export interface HttpResponse { + /** + * Gets the response status code. + */ + statusCode: number; - /** - * Gets or sets the request headers in JSON format. - */ - headers?: any; + /** + * Gets the response headers. + */ + headers: Headers; - /** - * Gets or sets the request body. - */ - content?: string | FormData; + /** + * Gets the response content. + */ + content?: HttpContent; + } - /** - * Gets or sets the request timeout in milliseconds. - */ - timeout?: number; - } + export type Headers = { [key: string]: string | string[] }; - /** - * Encapsulates HTTP-response information from an HTTP-request. - */ - export interface HttpResponse { - /** - * Gets the response status code. - */ - statusCode: number; + export const enum HttpResponseEncoding { + UTF8, + GBK + } + /** + * Encapsulates the content of an HttpResponse. + */ + export interface HttpContent { + /** + * Gets the response body as raw data. + */ + raw: any; - /** - * Gets the response headers. - */ - headers: Headers; + /** + * Gets the response body as string. + */ + toString: (encoding?: HttpResponseEncoding) => string; - /** - * Gets the response content. - */ - content?: HttpContent; - } - - export type Headers = { [key: string]: string | string[] }; + /** + * Gets the response body as JSON object. + */ + toJSON: (encoding?: HttpResponseEncoding) => any; - /** - * Encapsulates the content of an HttpResponse. - */ - export interface HttpContent { - /** - * Gets the response body as raw data. - */ - raw: any; + /** + * Gets the response body as ImageSource. + */ + toImage: () => Promise; - /** - * Gets the response body as string. - */ - toString: () => string; - - /** - * Gets the response body as JSON object. - */ - toJSON: () => any; - - /** - * Gets the response body as ImageSource. - */ - toImage: () => Promise; - - /** - * Gets the response body as file. - */ - toFile: (destinationFilePath?: string) => fs.File; - } + /** + * Gets the response body as file. + */ + toFile: (destinationFilePath?: string) => fs.File; + } } \ No newline at end of file