Is there an easy way to detect and decode all encoded characters in any url coming to my node app ?
can it be done with a middleware that fetch and decode symbols like & ?
First off, & shouldn't be in the URL that comes to your server. If it is, someone is likely double-encoding something.
Second, you wouldn't want to decode the URL with middleware unless the usage of that decoded data is only used in that middleware. What I mean to say is that you shouldn't modify the original URL, or other middlware may get confused.
Finally, JavaScript has decodeURIComponent() built-in. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURIComponent
Related
I have a GraphQL server, hosted on express. I want to return images to the client by sending back nodejs buffer objects. How can i config graphql server, to return bytes, instead of json? I don't wish to do this through base64, as the image are large in size.
You have to return JSON, but there's still a way. We're using GraphQL to return images stored in Blob fields in a legacy sql database. We're using sequelize, graphql-sequelize, and graphql-js.
We've defined the Blob fields to be String types in our graphql schema and so they come through in the json reply just fine.
Then we convert to a buffer before delivering, like
const imgBuffer = new Buffer.from(imgObj.data, 'ascii');
The only problem is we're now having trouble saving image data back to the database via the graphql interface. Our mutation function gives us a syntax error when it finds certain bad unicode characters in the strings, like \U0000 and whatnot (so I found your question looking for a solution to that).
There's a way, but it's complicated, and very manual, and I'm only going to give you an overview of what I've done in ApolloServer, but I think it should be enough.
First, you need to use the "Accept" header in your request to send a binary mime type, and send a matching "Content-Type" in your response. This is nessisary to be efficient, but not nessisary to work, as you'll see (with EJSON).
To serialize and deserialize respecting the headers you may need to write an express middleware, and you'll need to handle base64 encoding with a {$data: "..."} encapsulating object (as EJSON does) or just (strangely) returning null, if someone makes a request for binary data using "application/json" for their "accept" header. You'll also want to choose what binary formats that you'll support. I only use 1: "application/x-msgpack", but I hear that "application/cbor" is becoming more popular. You can use a library for EJSON, MessagePack, and CBOR to do your serialization, so this isn't as hard as it sounds.
I would then strongly recommend using the #defer on any images. See this post for more information on #defer: https://www.apollographql.com/blog/introducing-defer-in-apollo-server-f6797c4e9d6e/
I've done it. It wasn't easy, and it would be better if ApolloServer worked this way "out of the box".
It's better to send a hashed & temporary link to download it
The URL can be hashed to be non-accessible by other users.
Backend can expire the link or remove the binary file on the static server.
There might be an answer to your question by using the node module found here.
I have two html files, and the task for us is to pass data between the two. Then I came up with the idea of sending the data through the URL using the hash, and parsing this link something like JSON.parse(window.location.hash.slice(1)); and assigning it to a local variable. It seems to work for the couple try. But when I populated my JS files with codes error occurs. Can you tell what alternative can I do.? Here's the console errors. I'm using jquery by the way ..
The Console Error
Thank you!
JSON contains a number of characters that are not legal in urls.
A simple way around this could be to simply encode the JSON data using Base64.
You can use the latest way of accessing the data from one page to another:
//1st page
storage["key"]=data;
//2nd page
var value= storage["key"];
I think jQuery.param is what you need it converts a Json into a URL String
http://www.sourcecodemart.com/convert-json-object-to-url-query-string/
This won't work in the long run. urls are limited to about 2000 characters. What is the maximum length of a URL in different browsers?
You have to base64 encode the json to have it live in the URL. This eats up a lot of the available characters.
You don't get the same limitations when doing POST requests but a HTML page can't access post requests.
You might want to look at postMessage and embedding one page in the other in an iframe to do cross communication.
Also if the urls are on the same domain, just use local or session storage.
I'm writing an app that makes calls to Tesla's API and am trying to capture the base URL in a variable. The URL's all have a similar base:
https://owner-api.teslamotors.com/api/1/vehicles
All but one of the endpoints add further paths onto that URL, and it always starts by adding the vehicle ID. My question is, if I make a variable that has this as the base path:
https://owner-api.teslamotors.com/api/1/vehicles/
Would making a GET request to that URL function the same as making a GET request to the version without the trailing slash? This would help clean the code up a bit if so. I'm writing in Javascript if that matters.
Thanks for the help!
Yes, ..vehicles is not the same URI as ..vehicles/. Quite obviously they're different. Whether this matters for this particular URI depends on the URI/web server.
A web server is free to respond to any URI request in any manner possible. The server may or may not normalise the URI, it may or may not treat URIs with and without trailing slash identically, it may or may not treat it the same for some URIs but not for others. The only thing you can do is try with your particular URI on your particular web server.
I am making a bookmarklet that uses $.getJSON() to transfer data from browser (different domain) to server and back. and I am having several issues with url encoding.
When I send the data to php I use encodeURIComponent() and decodeURIComponent() the response. in the php i do no encoding or decoding.
In many situation this works fine in cases of "/ & etc.
However if i have ',' or % or some other character, when i get the response and it tries to decodeURIComponent. i get the following error :URIError: malformed URI sequence
Is there a better way to encode url ?
Thanks
Hey so I'm having trouble figuring out how to include something that looks URI encoded but in fact must be treated literally in my RESTful URL. For example, say I had an endpoint on my server that looked like this:
/something/:value
Then from my client code, I want to make a GET request to:
/something/some%20value
On the server, I want ":value" to be the literal string "some%20value" and NOT "some value". How do I properly encode the request URL to ensure the server treats it as such? I should also mention that not all request URIs will have these potential URL encoded values in them.
Thanks in advance.
Actually I think I may have figured this out. It seems to work if I just encode the percentage sign in my URLs. URL code for % is %25. So for example the correct URL would be:
/something/some%2520value