-
Notifications
You must be signed in to change notification settings - Fork 15.6k
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
How do I read a JSON query string? #2460
Comments
Instead of |
Express does not touch the URL in that way. Manually reading As for Here is how you would read that strange URL without Express at all (it will echo back the decoded value or 400: var http = require('http')
var url = require('url')
http.createServer(app).listen(4000)
function app(req, res) {
var query = url.parse(req.url).query
var obj
if (query) {
try {
obj = JSON.parse(decodeURIComponent(query))
} catch (e) {
res.statusCode = 400
res.end(e.toString())
return
}
res.setHeader('Content-Type', 'application/json')
res.end(JSON.stringify(obj, null, 2))
return
}
res.statusCode = 400
res.end('missing query')
} And with Express, doing the same as above: var express = require('express')
var url = require('url')
var app = express()
app.use(function (req, res, next) {
var query = url.parse(req.url).query
var obj
if (query) {
obj = JSON.parse(decodeURIComponent(query))
res.send(obj)
return
}
var error = new Error('missing query')
error.status = 400
next(error)
})
app.listen(4000) |
dear dougwilson, |
It's no problem: we're here to help and thanks for the detailed report of what you were trying to do so I could come up with something :) |
Dear developers,
I have a problem with express.js JSON string parsing coming from a GET requests.
On the client-side I use jQuery's datatables which produces valid JSON (I have checked) and sends it to my express.js server with contentType "application/json;charset=utf-8" and http GET method.
Here is the exact client-side code I use:
https://gist.github.com/OliPelz/7d46ac01e888b6fd2829
here is the extracted GET URL from loading the page above:
as decoded URL for better viewing experience:
or here the JSON string extracted and prettyprinted with JSLINT (which validated it as "Valid JSON")
https://gist.github.com/OliPelz/8cf40a2246f843321007
Now on the express.js server-side I use the normal mechanism to parse the received string from the GET request and process it:
The problem here is that the query parameters cannot be decoded, it is no valid JSON and it cannot be parsed, here is the server console output, note the weird json string
and following parse error (which is obious when looking at the received query.output)
Ive also tried with a "url" approach
the json request string looks different but is also not in right JSON format
different json string output:
resulting in the same error
I think the express reading/decoding mechanism of the raw url string has some bugs because I can make it work with the following quick fix by processing the "raw" URL parameters manually
The code above results in a working javascript object parsed from the JSON string which I can access correctly!
Output:
Is this a bug and can be confimed or am I doing something completely stupid here?
I am not behind some proxy, just developing on localhost without any network restrictions.
Thank you for your help
Best,
Oliver
The text was updated successfully, but these errors were encountered: