How can the submitted values in a POST message be accessed in Rust/Iron framework? - rust

I am using a form which uses the POST method to submit the input. Is it possible to access the input like accessing a dictionary? I think there must be a better way than parsing the request body.

You can use the params crate for this.
With this crate, you can get the POST parameters with:
let params = req.get_ref::<Params>();
(See the example for a complete code.)

Related

Difference between operators in a URL

What's the difference between using : and ? in a URL? For example /products/:id and /products?id=1? I am trying to get the values from the URL like this Product.findById (req.params.id) but I was wondering which one is most suitable. I know using : do I have to use req.params and ? req.query but I don't understand the difference between them, are they the same?
in my point of view, it is totally different if you are using RESTFUL API pattern
/products/:id called path parameters
The path parameters determine the resource you’re requesting for. Think of it like an automatic answering machine that asks you to press 1 for service, press 2 for another service, 3 for yet another service and so on.
Path parameters are part of the endpoint itself and are not optional
but query parameters
Technically, query parameters are not part of the REST architecture, and they used to help you completely understand how to read and use API’s Query parameters give you the option to modify your request with key-value pairs.
Having your parameters in the query is conceptually optional to the router, query parameters are more of properties and descriptions of the request itself, like when saying GET /users?sort=asc, in this case, the sort attribute was more of a description to the request and the request could complete the fetch without it, that might not always be the case, but a query parameter still describes its request even if it was mandatory.
On the other hand, URL parameters are part of the request itself, the URL without the parameter doesn't make sense, like GET /users/:userID, in this case, not supplying userID will supply unexpected data (A list of users for example) if it didn't break the router completely. URL parameters play part in defining the request rather than just describing it, and they can't be optional.

Is there a way to send an object over a GET request?

I wanna make a server-side database query based on the following data from the client:
const passengers = {
adults: 5,
teens: 1,
kids: 2,
babies: 3,
pets: 0
};
What I know
I'm aware that I can send all the properties as individual query parameters, but I'm going to be sending some other ones and I'm interested in:
Grouping some of the data I send.
The capability of sending more complex objects with nested properties.
I'm also aware that I can switch to a POST request that accepts a JSON body, but I was willing to stick with GET since the response is a list of the queried entity and I think that GET suits better that concept as POST is rather meant for creating elements.
Is using POST the only option or can I solve this with a GET request?
There's nothing stopping you from putting a json string in the query parameters, not ideal but I think it will be more reliable than adding a body to a get request

How to get user input in Yesod without using Yesod Form

In my page I have a search field(text area).
Yesod Form is quite complicated to use, so I figure I'll just use Javascript to get the value typed by the user. But then how my Haskell program gets that value?
You need to make a HTTP request to the server and pass it data.
In case of GET request, you just declare handler parameters in config/routes and then get them as getYourHandlerR arguments. In case of POST, you can use requireJsonBody to get data as JSON or getPostParams.

what's the wreq's way of creating custom cookies?

I want to make HTTP requests with some known cookie key-value pairs (e.g k1=v1; k2=v2; ...) using wreq, but can't find a convenient way of creating them.
Because Cookie has many fields, I'd prefer a smart constructor than having to fill all fields on my own.
By browsing documents, I find generateCookie to be the most promosing one (and that's also the only one I've found that returns a Cookie): I can create SetCookie and all I need are just key value pairs. But we don't have a Request to feed it as the second argument. Using http-client alone, one can do parseUrl to create one. But in wreq I feel the author want to hide Request from user and I can't find a function that gives us direct access to it.
So my question is: are there better ways to create cookies in wreq than using Cookie constructor?
I'd probably fork the generateCookie implementation, as it looks like the Request argument is used only to validate the fields.
http://hackage.haskell.org/package/http-client-0.4.26.2/docs/src/Network-HTTP-Client-Cookies.html#generateCookie, except for default cookie path, as SetCookie has Maybe path.

Postman converts Mongo arrays to Objects

I am working on an application that uses MongoDB for data persistence, alongside the mongoose library to allow angularjs to communicate with it. I'm using Postman to test my routes, and that's where the problem comes in.
In MongoDB, I inserted an entry thusly:
db.posts.insert{{id:6, author:"Blah", area:"Sport", body:"bleh", user_id:6, comments:[{author:"foo", body:"bar"}]})
I then queried it through Postman, and it successfully returned the Post, but there was a problem. The comments array had been returned as a strange data type, object Object, and I could not access it from my HTML files.
This is what Postman retrieved:
{"_id":"5536112cffc7bf00b2585d24","id":6,"author":"Blah","area":"Sport","body":"bleh","user_id":6,"comments":["[object Object]"]}]
I want to be able to access the author and body elements of comments independently, or else to be able to get at them from my http files. Does anyone know how I might stop this Object-ification from happening, or failing that, how I might query the data from this new Object?
Thanks.
Instead of passing complete db object we can pass required parameters. below is the example to form the required parameters.
res.json({"author":item.author,"body":item.body});
Here You can find call back function and array conversion from the cursor

Resources