Skip to content

Commit 4ef7e51

Browse files
committed
fix: needs to be url encoded
1 parent afd58a1 commit 4ef7e51

2 files changed

Lines changed: 7 additions & 6 deletions

File tree

packages/yasqe/src/__tests__/share-test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ describe("Share Functionality", () => {
134134
' "Accept" = "application/sparql-results+json"',
135135
" }",
136136
' ContentType = "application/x-www-form-urlencoded"',
137-
' Body = "query=$query"',
137+
' Body = "query=$([System.Net.WebUtility]::UrlEncode($query))"',
138138
' OutFile = "sparql-generated.json"',
139139
"}",
140140
"",
@@ -145,7 +145,7 @@ describe("Share Functionality", () => {
145145
expect(psString).to.include('$query = @"');
146146
expect(psString).to.include('"@');
147147
expect(psString).to.include(query);
148-
expect(psString).to.include('Body = "query=$query"');
148+
expect(psString).to.include('Body = "query=$([System.Net.WebUtility]::UrlEncode($query))"');
149149
expect(psString).to.not.include('Body = "query=`$query"'); // Should NOT escape the variable
150150
expect(psString).to.include("sparql-generated");
151151
});

packages/yasqe/src/sparql.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -546,15 +546,16 @@ export function getAsPowerShellString(yasqe: Yasqe, _config?: Config["requestCon
546546
}
547547

548548
// Build the body with the query variable and any other parameters
549-
// Note: We don't escape the variable reference itself, only the other args
549+
// The query must be URL-encoded for application/x-www-form-urlencoded
550550
let bodyExpression: string;
551+
const urlEncodeExpr = `[System.Net.WebUtility]::UrlEncode($${queryParamName})`;
551552
if (queryParam && Object.keys(otherArgs).length > 0) {
552553
// Both query variable and other args
553554
const otherArgsString = queryString.stringify(otherArgs);
554-
bodyExpression = `"${queryParamName}=$${queryParamName}&${escapePowerShellString(otherArgsString)}"`;
555+
bodyExpression = `"${queryParamName}=$(${urlEncodeExpr})&${escapePowerShellString(otherArgsString)}"`;
555556
} else if (queryParam) {
556-
// Only query variable
557-
bodyExpression = `"${queryParamName}=$${queryParamName}"`;
557+
// Only query variable - use subexpression for URL encoding
558+
bodyExpression = `"${queryParamName}=$(${urlEncodeExpr})"`;
558559
} else {
559560
// Only other args (shouldn't happen, but handle it)
560561
const otherArgsString = queryString.stringify(otherArgs);

0 commit comments

Comments
 (0)