I have an AJAX post request that will hit the Golang backend. The goal is to edit this request before sending the request to an outside api endpoint.
The ajax POST request example:
var ajaxParams = {
type: 'POST',
url: '/golang_endpoint', // golang backend endpoint
dataType: 'json',
data: encodeURIComponent(JSON.stringify(request)), // this is the form we want to send to an external endpoint
success: onResponse,
error: onError,
};
$.ajax(ajaxParams);
This request will hit the associated Golang handler, and we want to edit some of the request before sending it out. However, we are getting errors just sending the request without any edits:
func golangEndpointHandler(rw http.ResponseWriter, req *http.Request) {
fmt.Println(req.PostForm)
resp, err := http.PostForm("webwsite.com/outside/endpoint", req.PostForm)
}
Specifically, we are getting 500 Internal Server Errors sending the POST request (ex: unexpected token at '='
). Is using req.PostForm
the right way to forward our request data? The error indicates maybe something with decoding/encoding req.PostForm
or the data from the AJAX data param?
The print statement suggests a json serialization was performed: map[{"size":"1000","other_data":12345}:[]]
EDIT: per @belindamichaels response. I believe ParseForm
does some sort of encoding/decoding that causes 500 responses once sent to the outside endpoint.
While this combination works:
var ajaxParams = {
type: 'POST',
url: '/golang_endpoint'
dataType: 'json',
processData: false,
timeout: timeout,
data: encodeURIComponent(JSON.stringify(request)),
success: onResponse,
error: onError,
};
resp, err := http.Post("webwsite.com/outside/endpoint", req.Header.Get("Content-Type"), req.Body)
This does not:
var ajaxParams = {
type: 'POST',
url: '/golang_endpoint'
dataType: 'json',
processData: false,
timeout: timeout,
data: request, // not stringified or url-encoded
success: onResponse,
error: onError,
};
req.ParseForm()
resp, err := http.PostForm("webwsite.com/outside/endpoint", req.PostForm)
Despite no editing of the request, we are getting 500's (specifically unexpected token at 'object Object]='
). How can I stringify and urlencode the data once ready to send to the outside endpoint?
NOTE: the following combination also does not work:
var ajaxParams = {
type: 'POST',
url: '/golang_endpoint'
dataType: 'json',
processData: false,
timeout: timeout,
data: encodeURIComponent(JSON.stringify(request)), // the change
success: onResponse,
error: onError,
};
req.ParseForm()
resp, err := http.PostForm("webwsite.com/outside/endpoint", req.PostForm)
We get a similar 500 error: unexpected token at '='
To forward the request as is, use:
resp, err := http.Post("webwsite.com/outside/endpoint", req.Header.Get("Content-Type"), req.Body)
If you want to edit the request body, the server and client must agree on the type of the request body. Here's how to use application/x-www-form-urlencoded. Pass the form as is to the server. Don't encode the form as JSON as in the question.
var ajaxParams = {
type: 'POST',
url: '/golang_endpoint', // golang backend endpoint
dataType: 'json',
data: request,
success: onResponse,
error: onError,
};
Parse the form on the server, edit it and send it on.
req.ParseForm()
// edit form
resp, err := http.PostForm("webwsite.com/outside/endpoint", req.PostForm
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.