-
Notifications
You must be signed in to change notification settings - Fork 10.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The development proxy does not transfer non-200 HTTP codes and responses #36589
Labels
status: confirmed
Issue with steps to reproduce the bug that’s been verified by at least one reviewer.
topic: core
Relates to Gatsby's core (e.g. page loading, reporter, state machine)
type: bug
An issue or pull request relating to a bug in Gatsby
Comments
jsalvata
added
the
type: bug
An issue or pull request relating to a bug in Gatsby
label
Sep 11, 2022
gatsbot
bot
added
the
status: triage needed
Issue or pull request that need to be triaged and assigned to a reviewer
label
Sep 11, 2022
jsalvata
added a commit
to jsalvata/gatsby
that referenced
this issue
Sep 11, 2022
Transfer errors responses from the proxied server, including the status code, to the client. Fixes gatsbyjs#36589, aka gatsbyjs#34244, aka gatsbyjs#33333.
I've submitted a pull request with dangkyokhoang's fix: |
LekoArts
added
status: confirmed
Issue with steps to reproduce the bug that’s been verified by at least one reviewer.
topic: core
Relates to Gatsby's core (e.g. page loading, reporter, state machine)
and removed
status: triage needed
Issue or pull request that need to be triaged and assigned to a reviewer
labels
Sep 12, 2022
jsalvata
added a commit
to jsalvata/gatsby
that referenced
this issue
Oct 3, 2022
Transfer errors responses from the proxied server, including the status code, to the client. Fixes gatsbyjs#36589, aka gatsbyjs#34244, aka gatsbyjs#33333.
jsalvata
added a commit
to jsalvata/gatsby
that referenced
this issue
Oct 13, 2022
Transfer errors responses from the proxied server, including the status code, to the client. Fixes gatsbyjs#36589, aka gatsbyjs#34244, aka gatsbyjs#33333.
Looks like the pull request was denied and the original issue was closed. This is still an issue for me so I'm following this ticket in hopes for a fix. |
I have came with simple workaround. I wrote a function (extraced from patch) in my const got = require('got');
const report = require("gatsby-cli/lib/reporter");
function proxy(app, prefix, url) {
app.use(`${prefix}/*`, (req, res) => {
const proxiedUrl = url + req.originalUrl
const {
headers: { host, ...headers },
method,
} = req
req
.pipe(
got
.stream(proxiedUrl, {
headers,
method: method,
decompress: false,
})
.on(`response`, response =>
res.writeHead(response.statusCode || 200, response.headers)
)
.on(`error`, (err, _, response) => {
if (response = err.response) {
res.writeHead(response.statusCode || 400, response.headers)
res.end(response.rawBody)
} else {
const message = `Error when trying to proxy request "${req.originalUrl}" to "${proxiedUrl}"`
report.error(message, err)
res.sendStatus(500)
}
})
)
.pipe(res)
})
} and changed proxy config in module.exports = {
proxy: {
prefix: "/api",
url: "http://localhost:8080"
}
} to: module.exports = {
developMiddleware: (app) => {
proxy(app, "/api", "http://localhost:8080");
}
} I hope this helps. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
status: confirmed
Issue with steps to reproduce the bug that’s been verified by at least one reviewer.
topic: core
Relates to Gatsby's core (e.g. page loading, reporter, state machine)
type: bug
An issue or pull request relating to a bug in Gatsby
Preliminary Checks
Description
When using the embedded HTTP proxy to access an API as instructed in https://www.gatsbyjs.com/docs/api-proxy/, any non-200 responses from the remote server will be passed to the client as "500 Internal server error". The original response from the remote server is lost.
This has been reported in detail at least twice:
#34244 (moved to a discussion)
#33333 (closed following the release of Gatsby 4, in spite of dangkyokhoang having provided a solution)
Reproduction link and steps to reproduce can be found in the 2nd link above. I'll copy them over.
Reproduction Link
https://codesandbox.io/s/infallible-phoebe-69w1t?file=/src/pages/index.js
Steps to Reproduce
gatsby-config.js
Expected Result
Should receive 400 {"error":1}
Actual Result
Received 500 Internal Server Error
Environment
Config Flags
No response
The text was updated successfully, but these errors were encountered: