Fix http request format (#22618)

* fix http request format

* encode to utf 8 and add body for put requests

* encode proxy request body

* change ?.data to ?.body

* add note for textencoder

* change content-type to application/json
This commit is contained in:
Christopher Suh
2023-04-10 14:36:35 -07:00
committed by GitHub
parent e7a5ea6ee8
commit 6857718cb3
2 changed files with 10 additions and 7 deletions

View File

@@ -7,6 +7,7 @@
import { INetworkModule, NetworkRequestOptions, NetworkResponse } from '@azure/msal-common'; import { INetworkModule, NetworkRequestOptions, NetworkResponse } from '@azure/msal-common';
import * as http from 'http'; import * as http from 'http';
import * as https from 'https'; import * as https from 'https';
import { TextEncoder } from 'util';
import { NetworkUtils } from './networkUtils'; import { NetworkUtils } from './networkUtils';
/** /**
@@ -151,9 +152,10 @@ const networkRequestViaProxy = <T>(
// compose a request string for the socket // compose a request string for the socket
let postRequestStringContent: string = ''; let postRequestStringContent: string = '';
if (httpMethod === HttpMethod.POST || httpMethod === HttpMethod.PUT) { if (httpMethod === HttpMethod.POST || httpMethod === HttpMethod.PUT) {
const body = options?.body || ''; // Note: Text Encoder is necessary here because otherwise it was not able to handle Chinese characters in table names.
const body = (new TextEncoder()).encode(JSON.stringify(options?.body || ''));
postRequestStringContent = postRequestStringContent =
'Content-Type: application/x-www-form-urlencoded\r\n' + 'Content-Type: application/json\r\n' +
`Content-Length: ${body.length}\r\n` + `Content-Length: ${body.length}\r\n` +
`\r\n${body}`; `\r\n${body}`;
} }
@@ -284,7 +286,8 @@ const networkRequestViaHttps = <T>(
): Promise<NetworkResponse<T>> => { ): Promise<NetworkResponse<T>> => {
const isPostRequest = httpMethod === HttpMethod.POST; const isPostRequest = httpMethod === HttpMethod.POST;
const isPutRequest = httpMethod === HttpMethod.PUT; const isPutRequest = httpMethod === HttpMethod.PUT;
const body: string = options?.body || ''; // Note: Text Encoder is necessary here because otherwise it was not able to handle Chinese characters in table names.
const body = (new TextEncoder()).encode(JSON.stringify(options?.body || ''));
const url = new URL(urlString); const url = new URL(urlString);
const optionHeaders = options?.headers || {} as Record<string, string>; const optionHeaders = options?.headers || {} as Record<string, string>;
let customOptions: https.RequestOptions = { let customOptions: https.RequestOptions = {
@@ -319,7 +322,7 @@ const networkRequestViaHttps = <T>(
}); });
} }
if (isPostRequest) { if (isPostRequest || isPutRequest) {
request.write(body); request.write(body);
} }

View File

@@ -501,7 +501,7 @@ export async function createResourceGroup(account: AzureAccount, subscription: a
const host = getProviderMetadataForAccount(account).settings.armResource.endpoint; const host = getProviderMetadataForAccount(account).settings.armResource.endpoint;
const response = await makeHttpRequest(account, subscription, path, HttpRequestMethod.PUT, requestBody, ignoreErrors, host); const response = await makeHttpRequest(account, subscription, path, HttpRequestMethod.PUT, requestBody, ignoreErrors, host);
return { return {
resourceGroup: response?.response?.data, resourceGroup: response?.response?.body,
errors: response.errors ? response.errors : [] errors: response.errors ? response.errors : []
}; };
} }
@@ -511,8 +511,8 @@ export async function getStorageAccountAccessKey(account: AzureAccount, subscrip
const host = getProviderMetadataForAccount(account).settings.armResource.endpoint; const host = getProviderMetadataForAccount(account).settings.armResource.endpoint;
const response = await makeHttpRequest(account, subscription, path, HttpRequestMethod.POST, undefined, ignoreErrors, host); const response = await makeHttpRequest(account, subscription, path, HttpRequestMethod.POST, undefined, ignoreErrors, host);
return { return {
keyName1: response?.response?.data?.keys[0].value ?? '', keyName1: response?.response?.body?.keys[0].value ?? '',
keyName2: response?.response?.data?.keys[0].value ?? '', keyName2: response?.response?.body?.keys[0].value ?? '',
errors: response.errors ? response.errors : [] errors: response.errors ? response.errors : []
}; };
} }