Skip to content

Commit d984196

Browse files
committed
change POST request content to encoded HTTP form format
Signed-off-by: rubywtl <[email protected]>
1 parent 61e32f5 commit d984196

File tree

6 files changed

+25
-22
lines changed

6 files changed

+25
-22
lines changed

pkg/api/queryapi/query_api.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package queryapi
33
import (
44
"context"
55
"fmt"
6-
"io"
76
"net/http"
87
"time"
98

@@ -111,11 +110,8 @@ func (q *QueryAPI) RangeQueryHandler(r *http.Request) (result apiFuncResult) {
111110
endTime := convertMsToTime(end)
112111
stepDuration := convertMsToDuration(step)
113112

114-
byteLP, err := io.ReadAll(r.Body)
115113
if q.distributedExecEnabled {
116-
if err != nil {
117-
return apiFuncResult{nil, &apiError{errorBadData, err}, nil, nil}
118-
}
114+
byteLP := []byte(r.PostFormValue("plan"))
119115
if len(byteLP) != 0 {
120116
logicalPlan, err := logicalplan.Unmarshal(byteLP)
121117
if err != nil {
@@ -205,10 +201,7 @@ func (q *QueryAPI) InstantQueryHandler(r *http.Request) (result apiFuncResult) {
205201
tsTime := convertMsToTime(ts)
206202

207203
if q.distributedExecEnabled {
208-
byteLP, err := io.ReadAll(r.Body)
209-
if err != nil {
210-
return apiFuncResult{nil, &apiError{errorBadData, err}, nil, nil}
211-
}
204+
byteLP := []byte(r.PostFormValue("plan"))
212205
if len(byteLP) != 0 {
213206
logicalPlan, err := logicalplan.Unmarshal(byteLP)
214207
if err != nil {

pkg/api/queryapi/query_api_test.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package queryapi
22

33
import (
4-
"bytes"
54
"context"
65
"errors"
76
"fmt"
87
"io"
98
"net/http"
109
"net/http/httptest"
10+
"net/url"
11+
"strings"
1112
"testing"
1213
"time"
1314

@@ -457,8 +458,13 @@ func Test_Logicalplan_Requests(t *testing.T) {
457458
}
458459
}
459460

460-
func createTestRequest(path string, body []byte) *http.Request {
461-
req := httptest.NewRequest(http.MethodPost, path, io.NopCloser(bytes.NewReader(body)))
461+
func createTestRequest(path string, planBytes []byte) *http.Request {
462+
form := url.Values{}
463+
form.Set("plan", string(planBytes))
464+
req := httptest.NewRequest(http.MethodPost, path, io.NopCloser(strings.NewReader(form.Encode())))
465+
466+
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
467+
462468
ctx := context.Background()
463469
_, ctx = stats.ContextWithEmptyStats(ctx)
464470
return req.WithContext(user.InjectOrgID(ctx, "user1"))

pkg/querier/tripperware/instantquery/instant_query.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,24 +183,27 @@ func (c instantQueryCodec) EncodeRequest(ctx context.Context, r tripperware.Requ
183183
}
184184
}
185185

186-
h.Add("Content-Type", "application/json")
186+
h.Add("Content-Type", "application/x-www-form-urlencoded")
187187

188188
isSourceRuler := strings.Contains(h.Get("User-Agent"), tripperware.RulerUserAgent)
189189
if !isSourceRuler {
190190
// When the source is the Ruler, skip set header
191191
tripperware.SetRequestHeaders(h, c.defaultCodecType, c.compression)
192192
}
193193

194-
byteBody, err := c.getSerializedBody(promReq)
194+
bodyBytes, err := c.getSerializedBody(promReq)
195195
if err != nil {
196196
return nil, err
197197
}
198+
form := url.Values{}
199+
form.Set("plan", string(bodyBytes))
200+
formEncoded := form.Encode()
198201

199202
req := &http.Request{
200203
Method: "POST",
201204
RequestURI: u.String(), // This is what the httpgrpc code looks at.
202205
URL: u,
203-
Body: io.NopCloser(bytes.NewReader(byteBody)),
206+
Body: io.NopCloser(strings.NewReader(formEncoded)),
204207
Header: h,
205208
}
206209

pkg/querier/tripperware/instantquery/instant_query_middlewares_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,7 @@ func TestRoundTripWithAndWithoutDistributedExec(t *testing.T) {
214214
require.NoError(t, err)
215215

216216
// check request body
217-
body, err := io.ReadAll(req.Body)
218-
require.NoError(t, err)
217+
body := []byte(req.PostFormValue("plan"))
219218
if tc.expectEmptyBody {
220219
require.Empty(t, body)
221220
} else {

pkg/querier/tripperware/queryrange/query_range.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,7 @@ func (c prometheusCodec) EncodeRequest(ctx context.Context, r tripperware.Reques
189189
h.Add(n, v)
190190
}
191191
}
192-
193-
h.Add("Content-Type", "application/json")
192+
h.Add("Content-Type", "application/x-www-form-urlencoded")
194193

195194
tripperware.SetRequestHeaders(h, c.defaultCodecType, c.compression)
196195

@@ -199,11 +198,15 @@ func (c prometheusCodec) EncodeRequest(ctx context.Context, r tripperware.Reques
199198
return nil, err
200199
}
201200

201+
form := url.Values{}
202+
form.Set("plan", string(bodyBytes))
203+
formEncoded := form.Encode()
204+
202205
req := &http.Request{
203206
Method: "POST",
204207
RequestURI: u.String(), // This is what the httpgrpc code looks at.
205208
URL: u,
206-
Body: io.NopCloser(bytes.NewReader(bodyBytes)),
209+
Body: io.NopCloser(strings.NewReader(formEncoded)),
207210
Header: h,
208211
}
209212

pkg/querier/tripperware/queryrange/query_range_middlewares_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,7 @@ func TestRoundTripWithAndWithoutDistributedExec(t *testing.T) {
233233
require.NoError(t, err)
234234

235235
// check request body
236-
body, err := io.ReadAll(req.Body)
237-
require.NoError(t, err)
236+
body := []byte(req.PostFormValue("plan"))
238237
if tc.expectEmptyBody {
239238
require.Empty(t, body)
240239
} else {

0 commit comments

Comments
 (0)