Pass a resource in URI as a parameter Ruby API - ruby

I hope this is not a question dealing with passing a parameter in a URL.
I have a Put request. The following code adds a user named "Johndoe" in the server. It is NOT like name ?= 'JohnDoe' (not passed as a parameter)
uri = URI.parse('http://localhost:15672/api/users/JohnDoe/')
I would like to have the JohnDoe or any name passed from a function like,
def test_add_user(username,password)
uri = URI.parse('http://localhost:15672/api/users/**username**/')
The username in the URL is the username getting added as a user in the server. I suppose the username is not a URI parameter since the 'name' part looks like a resource to me. I tried the following code but in vain
uri.join('http://localhost:15672/api/users/',username)
Also I tried this
username = 'john/'
uri.join('http://localhost:15672/api/users/',username)
Any fixes or any alternative ideas as to how I can pass the name?
Regards
Karthik

It could be done by interpolation of username, like this:
uri = URI.parse("http://localhost:15672/api/users/#{username}")

Related

how to pass static value through url in blade file?

I want to send static value through my url from my blade file .But this not working.can you give me the solution
>
> This is link
you have syntax error you can fix by
This is link
here ?rfq=1 this will come under '' this tag as it is string
as your router is defined as this
Route::get('/user-login/{email}/{password}', 'UserController#user_login')->name('users.user_login');
you should use route name and pass the data like this
This is link
then if you need to pass you can use like this
This is link
this will generate url like localhost:8000/user-login/example#mail.com/1234?rfq=1"
ref link https://laravel.com/docs/8.x/helpers#method-route
If you are using {} in your route(route parameter method) then no need to have '?' in your URL. If you need 127.0.0.1:8000/user-login?rfq=1 as your URL then create route without route parameters as
Route::get('/user-login,'UserController#user_login')->name('users.user_login');

Mapping of missing URI variables to Request Mapping

I've developed a Spring API /getFileData, which accepts three URI parameters viz. businessDate/fileName/recordId. It is possible to have any of them can be passed as null. But I still want my API to be working in this case also. How can I achieve this?
I've tried using #GetMapping("getFileData/{businessDate}/{fileName}/{recordId}", "getFileData/{businessDate}//", "getFileData/{businessDate}/{fileName}/")..so on like this for all possible combinations.
#RequestMapping(value = "/getFileData/{businessDate}/{fileName}/{recordId}", method = RequestMethod.GET)
I want this API to be working for all the combination of URI parameters if something get missed out. for example someone requested,
/getFileData///22 or
/getFileData/22Dec2018/ or
/getFileData//treasure/22
You can do that with a #RequestParam of type java.util.Map.
With your design, you will have various #PathVariable params in the controller method as well as the order of path variables /{var1}/{var2}... constructs the url so I don't think it would be possible to skip a path variable in the url and still call the same controller method.

Laravel How to get Url

When trying to get page url with
URL::current();
it's ignore the pagination.
For example:
current url is www.google.com/searc?page=2
dd(URL::current());
gives us www.google.com/search. Using default pagination.
My question is: Any function for get url with paginations?
Simply use :
\URL::full();
It will give the url along with query string.
You will need the full URL, not just the URL. Try this:
\URL::full();
As described in the docs of Laraval. You can use URL::full()
Accessing The Current URL
If no path is provided to the url helper, a
Illuminate\Routing\UrlGenerator instance is returned, allowing you to
access information about the current URL:
// Get the current URL without the query string...
echo url()->current();
// Get the current URL including the query string...
echo url()->full();
// Get the full URL for the previous request...
echo url()->previous();
Each of these methods may also be accessed via the URL facade:
use Illuminate\Support\Facades\URL;
echo URL::current();

How should you get correct id based on username for a user for a public profile view?

I'm trying to figure out how to view a profile-page in codeigniter. I know how to create templates and so on in CodeIgniter, but I don't know which way to go to handle "translation from username to userid" in a secure manner. This is a profile that should be viewed when the user isn't logged in (public profile).
Code snippet from my profile-controller:
$username = uri_segment(3); //Here's the actual username is stored in the url
$profile = new Profile();
$this->user_id = $profile->getUserByUserName($username);
Above seems ok, right? But when a username contains spaces, the "username-link" gets from "Gustav Wiberg" to "gustav-wiberg" (space replaced by hyphens, and the username is lowercase).
The actual link could look something like this: http://www.domain.com/profile/gustav-wiberg
But if I check this link with uri_segment I get username "gustav-wiberg" and not "Gustav Wiberg" which is the REAL username I want to compare with (by getting the return value of $profile->getUserByUserName($username);.
Am I taking the wrong approach to this? Or is it ok approach based on each username must be unique.
I could just do a replacement from uri_segment "gustav-wiberg" by capitalizing each word and replacing hypens with space and then do my query to check id for that username?
Please give me any pointers...
Either you can put a check while creating the username that there should not be spaces i.e. User can create a URL friendly username.
Or as you mentioned, you can do a replacement from uri_segment. But thinking of different scenarios, this approach can create issues so it seems its better to create a url friendly username.
One more option is you can add one more field in DB along with the username, say "slug". Wherein you can convert the username to URL friendly name and store. And check for that name while retrieving.

How to handle encrypted URL's in rails?

I am sending email to user, in that email one link is there to redirect that user to rails application. I want that link to be in encrypted form with domain name for example:
https://www.domain_name.com?hdurstihnzdfalgfgdfhdrbnhduolsasrtyumyrtyr
when user click on this link, he should directly redirect to controller method we specified in that URL which is not visible.
Controller and methods given in URL may vary according to user.
So my question is how we can do this in rails.
If I encrypt controller name, method name and parameter we passed. How routes file come to know where to redirect this URL? How to decrypt this in routes file and redirect internally to decrypted URL?
Life will be easier if you can do a slight modification to your url, something like:
https://www.domain_name.com/enc/hdurstihnzdfalgfgdfhdrbnhduolsasrtyumyrtyr
Then you can create a route for that path to redirect where you want.
get '/enc/:encoded_path' => "controller#action"
This would give you access to params[:encoded_path], which would equal hdurstihnzdfalgfgdfhdrbnhduolsasrtyumyrtyr in this case. From there, you could decode in the controller and then redirect however you want.
That's not the right approach. Here's what you can do instead:
Create a new controller action, say for instance, activate.
def activate
activation_token = params[:auth_token]
....
your logic to do whatever with this token
end
Create a corresponding route:
match '/activate' => 'your_awesome_controller#activate'
Now, when you email your users, I'm guessing you're sending some sort of activation token. If not, create two new fields in your users table:
activation_token:string
activated:boolean
Use some unique string generation algorithm to generate your activation_token and email it to your user:
yourdomain.com/activate?auth_token=user.activation_token

Resources