Try to build an API using express and was told look into URLs with the form /path/:id/extension.
The path and extension are made up, but what i was sent follows that format. I have never seen anything with the : in the route.
Can anybody interpret this and tell me what it means? Is this standard practice?
Any help would be awesome!
The :id refers to req.params() object. Effectively it is how you pass variable data, eg. you would replace :id with the specific id you are referring to in the URL path, and build your id-specific logic around it, referring to the value as req.params.id.
See the Express Documentation for more detail.
This is standard syntax does Express, the colon prefix denotes a parameter variable.
In the route handler, the URL pattern you describe would match /path/5/extension, where 5 is now accessible through req.params.id. You can read more in the ExpressJS docs under Routing.
Related
I have a path like this:
GET http://aaa.com/invoices/{someType}/<bunch of optional query parameters>
It is okay to mix path parameters with regular query parameters in rest api?
Or better use required query parameters maybe there is better approach?
It is okay to mix path parameters with regular query parameters in rest api?
Yes. REST doesn't care what spelling you use for your resource identifiers -- any spelling that complies with the production rules described by RFC 3986 is fine. Information encoded into the URI is done at the server's discretion and for its own use.
From the perspective of a general purpose client, the identifier is the entire URI.
/a/b/c/d
/a/b/c/d?hasOptionalParameter=true
As far as REST is concerned, these are two different identifiers, and therefore two different resources. That you have a single endpoint for them is an implementation detail.
URI Templates allow you to describe "a range of Uniform Resource Identifiers through variable expansion." General purpose templates support variable expansion on both path segments and the query part.
But: one of the most familiar URI Templates is an HTML form; the processing rules for GET perform a replacement of the query part of the form action, but leaves unchanged the path segments. In effect, the path part of the form action URI is protected from change by the client, but the query part gets changed.
I'm converting an API using the Slim PHP framework to CGI. Slim handles route parameters very nicely (e.g. www.mysite.com/processorder/38929/w1?a1=test where 38929 and w1 are route parameters and includes additional GET parameters).
What is the correct or best way to do this? Do I need to configure httpd.conf to somehow convert the route parameters to POST parameters or additional GET parameters before it calls my CGI program?
I'm using the the CGIC C library at boutell.com as a starting point, if that matters.
Thanks!
Well I feel a little foolish. The answer was obvious if I had taken my head out of CGI for a second.
Since the site is served with Apache, I just needed to create an .htaccess file to convert the "route" parameters to GET parameters, including a QSA modifier to retain the original GET parameters in the URL (a1 in my example).
I am using RestSharp to access a RubyOnRails API.
As you might know, RoR likes when the parameters names are in the form model_name[property]. RestSharp, on the other hand, does not like it.
Fiddler says I send this to the server :
user%5Bemail%5D=user%40email.com&user%5Bpassword%5D=test
It looks like R# encodes both the parameters and values when it sends the data (unlike Curl, which looks like it encodes selectively).
While that's fine most of the time I guess, in this particular case, it makes the API return a 401 because it doesn't understand the parameters.
Is it possible to ask R# to not encode the request's parameters ?
Thank you !
Edit
Ok, in the R# sources, I found the method RestClient.EncodeParameters, so it looks like the parameter's name is always encoded. I guess I will have to fork it :(
Since RestSharp version 106.4.0 you can just use ParameterType.QueryStringWithoutEncode in request.AddParameter() function:
request.AddParameter("user_id", #"45454545%6565%65", ParameterType.QueryStringWithoutEncode);
I know this has already been answered, but I wanted to add this answer since it worked for me. There is an (albeit hacky) solution to this. Build your own uri for parameters that should not be encoded.
var uri = string.Concat("/path/to/your/api", "?paramThatShouldNotBeEncoded=", DateTime.Now.Date.AddDays(1).ToString("O"));
var restRequest = new RestRequest(uri, Method.GET);
In the RestSharp sources I found out that the parameters are always encoded (both the name and the value), so I guess that I will have to fork it if I want to add an additional parameter.
See this PR from the project site:
https://github.com/restsharp/RestSharp/pull/1157
However, as far as I can tell, it's not yet in a release on NuGet.
Update: probably doesn't work in most cases from comments.
I found an interesting solution... Just decode the parameters you want to pass in and restsharp will encode back to what it should be. For example, I have an api key that uses %7 in it and RestSharper further encodes it. I decoded the api key and passed that into RestSharp and it seems to work!
This solution worked for me
request.AddQueryParameter("sendEndDate", "string:data,something-else", false);
This is the function in the metadata of RestSharp.IRestRequest:
IRestRequest AddQueryParameter(string name, string value, bool encode);
i'm trying to use cross domain jsonp. i have done this before using the callback function in the json file from the other domain. i'm looking at an example json data file that google uses in one of its tutorials:
http://earthquake.usgs.gov/earthquakes/feed/geojsonp/2.5/week -- here obviously the callback function here is eqfeed_callback. in the json file i'm trying to use, there is no callback function that kicks everything off, there is just a bracket [. the file starts off like:
[{"Address":"4441 Van Nuys Blvd","City":"Sherman Oaks" ...
and ends like:
}]
what should i do? is there another way to get at the data without a callback function? i can't edit this file. it's a service that i have a subscription to.
thx.
If it's not your server, and the server doesn't support JSONP, there's no way you can force it to return jsonp. You could try adding ?callback=callback to your url to see if that convinces the server to wrap it in a callback, but if it doesn't, you're out of luck.
Well, almost. There is actually a really dirty hack that you shouldn't use, which is to override javascript's standard Array constructor to assign the contents of the array to a global variable. But that's pretty hideous and I strongly advise against it.
Better ask the maintainer of the service if they're willing to support JSONP. Or better yet, add a CORS header.
i am using Transloadit to process and store my pic to amazon s3. The upload works fine, however upon successful redirect back to my app i get an error when trying to access one of the vales from the hash of params sent by transloadit.
<%= params[:transloadit][:ok] %>
The error returned is
can't convert Symbol into Integer
and the hash of params looks like this:
{"transloadit"=>"{\"ok\":\"ASSEMBLY_COMPLETED\",
\"message\":\"The assembly was successfully completed.\",
\"assembly_id\":\".........\",
\"assembly_url\":\"http://api2.donnie.transloadit.com/assemblies/....\",
\"bytes_received\":351697,
\"bytes_expected\":351697,.........}
I am using the gem transloadit/rails-sdk for easy integration into my app. On their github page they say and i quote:
"If you want to use the automatic transload parameter decoding, you have to include the Transloadit::Rails::ParamsDecoder module into your controller
class YourController
include Transloadit::Rails::ParamsDecoder
end
that way the param[:transloadit] is automatically decoded for you, if it exists"
I am not sure what they mean by this (even if i include this into my controller i get an error with a different set of params). What is the purpose of this line?
All i need is to access the params[:transloadit][:ok] parameter. How can i get hold of this parameter? thanks
I had a similar problem. If you use key names instead of symbols, it might help. I'm not sure why, but that's what I had to do. Try params["transloadit"]["ok"] or some variation of that.