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 * as http from 'http';
import * as https from 'https';
import { TextEncoder } from 'util';
import { NetworkUtils } from './networkUtils';
/**
@@ -151,9 +152,10 @@ const networkRequestViaProxy = <T>(
// compose a request string for the socket
let postRequestStringContent: string = '';
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 =
'Content-Type: application/x-www-form-urlencoded\r\n' +
'Content-Type: application/json\r\n' +
`Content-Length: ${body.length}\r\n` +
`\r\n${body}`;
}
@@ -284,7 +286,8 @@ const networkRequestViaHttps = <T>(
): Promise<NetworkResponse<T>> => {
const isPostRequest = httpMethod === HttpMethod.POST;
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 optionHeaders = options?.headers || {} as Record<string, string>;
let customOptions: https.RequestOptions = {
@@ -319,7 +322,7 @@ const networkRequestViaHttps = <T>(
});
}
if (isPostRequest) {
if (isPostRequest || isPutRequest) {
request.write(body);
}