Retrieving all querystring variables from request object - django

In my django view, i have logic that retrieves a querystring variable called url from the request object like so:
link: http://mywebsite.com/add?url=http://www.youtube.com/watch?v=YSUn6-brngg&description=autotune-the-news
url = request.Get.get("url")
The problem arises, for example,when the url variable itself contains parameters(or variables)
link:http://mywebsite.com/add?url=http://www.youtube.com/watch?v=YSUn6-brngg&feature=SeriesPlayList&description=autotune-the-news
The feature parameter will be treated as a seperate variable. Since i don't always know the parameters that would be included inside the url variable, how can i force it to retrieve everything that comes before the description variable?

This is a URL encoding problem. Whatever technology is being used to generate the request needs to URL-encode the value for the 'url' parameter. This will make your link look like:
http://mywebsite.com/add?url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DYSUn6-brngg%26feature%3DSeriesPlayList&description=autotune-the-news
Now, Django will be able to parse the 'url' parameter completely without getting confused about the 'feature' and 'description' parameters. So, all you have to do is figure out how to get the UI technology used to create the link to encode that parameter.

Related

How to get url parameters as a list in Django?

I want to get url parameters as list in django. Say for example, I will add each parameters to url as;
mydomain.com/param1/param2/param3/.../paramx
Where each param may be existed or not. For example a link may be;
mydomain.com/param1/param3/param4/...
So my question is, How can I get list of params in Django?
I tried handling parameters manually but since they are seperated it doesn't work as expected.
looking at the docs you should be able to use path https://docs.djangoproject.com/en/4.0/topics/http/urls/#path-converters
you then most likely have the path/string param1/param2... as a variable on your view

How to post an object in postman without FromBody?

Let's say my controller is taking an object parameter without [FromBody], how do I post the data using Postman? I tried this way but it doesn't reach the endpoint.
I think in this case, since you're not using [fromBody] your object should be in the URL and not in the Request Body.
in Postman, this can be done using the Query Params as the screenshot shows.
Yet, I don't think you could pass an object in the query params like that. I think you should instead turn it into a query string like this format
"User=abc&Pass=abc"
Multiple ways to achieve this can be found here
If your controller is taking an object parameter without [FromBody] then you must send the data as URL parameter:
POST http://localhost:44364/login?object={"User":"abc","Pass":"abc"}
Note: replace "object" by the name of the parameter in your controller. Also you have to know that Postman should converts special characters { " : , to %XX format and you have to manage that in your service.
If you want to send it in the body, then include [FromBody]

How to pass a parameter date to url dispatcher but as named parameter

I've been reading lots of tutorials and stackoverflow questions, but I haven't found how to do this. I need to pass a parameter of type Date in a GET call on the URL.
I know it sounds easy, but I'm new to Django and what I have found is mostly something like this:
Django url that captures yyyy-mm-dd date
All those answers solve the issue if I want to have an URL like
http:www.myweb.com/2021/09/02
That is great if I want to write an url for a blog or website, but I'm doing it for an endpoint, and in my case, I need something like this
/some-action-endpoint/?created_at_gte=2020-01-01
So, I need to capture a parameter (hopefully named created_at_gte) with the value 2020-02-01 (It will awesome if I could capture that immediately as a Date object, but I'm fine with a string)
So, my questions are:
1.- It will be enough to create a group like this
url(r'^my-endpoint/(?P<created_at_gte>[(\d{2})[/.-](\d{2})[/.-](\d{4})]{4})/$', views.my_view),
Note_: by the way, the previous one is not working, if someone knows why it'll greatly appreciate it.
2.-The endpoint needs two parameters: created_at_gte and created_at_lte. How can I handle this? Do I need to add two url's to the url pattern?
I'm using Django 1.11 and Python 2.7. Cannot use other versions.
Query parameters aren't part of the URL matching behavior, so unfortunately your requirement to handle them as query parameters instead of URL path elements means you have to do more on your own rather than relying on pattern matching to reject bad requests.
You can do what you want as long as your base URL pattern (^my-endpoint/ in your example) is unambiguous and you're willing to handle the possibility that the parameters may not be set or may be set to something that doesn't parse as a date in your view, but you won't get the regexp-derived guarantees or the ability to bind them to view function parameters.
Instead, you'll have to extract them from the request.GET QueryDict object. That would look something like this:
In your url patterns:
url(r'^my-endpoint/$', views.my_view),
And then in your views:
def my_view(request):
created_at_start = request.GET.get("created_at_gte")
created_at_end = request.GET.get("created_at_lte")
# you now have no guarantees about these values except that if not None they'll be strings
# remember to do something appropriate for None, empty strings, strings that aren't dates, strings where end is before the start, etc

URL encode Postman variable?

I use Postman for REST API testing and parametrize tests with global variables.
I should put a phone number into GET request: /path/get?phone={{phone}} but leading + sign in the phone number is interpreted as a space.
What is the syntax to URL encode global variables in Postman? Is it possible to run JS encodeURIComponent() on variable in URL?
I am late but still worth it:
Just highlight and right click the part of url you want to encode. Select encodeURIComponent
That's it.
Use the Pre-request scripts (it's next to body) for this:
var encoded = encodeURIComponent({{phone number}});
or
var encoded = encodeURIComponent(pm.environment.get("phone number"));
and to proceed, use:
pm.environment.set("encoded phone number", encoded);
And set your URL to /path/get?phone={{encoded phone number}}
Just a shortcut to Mohhamad Hasham' answer.
You can encode and decode direct in the Params Value field:
The trick is to get your environment variable in the pre-request script and then set it after encoding it
var encoded = encodeURIComponent(pm.environment.get("phone"));
pm.environment.set("encoded phone number", encoded);
This will work as well:
var encoded = encodeURIComponent(pm.request.url.query.get("phone"));
pm.request.url.query.remove("phone");
pm.request.url.query.insert("phone", encoded);
I came across this question looking for an answer to a similar question. For me, the variable was a JSON object. The endpoint I needed to hit was expecting an object list as a query parameter and I have no way to change that to be the request body.
As much as some of the answers helped, I ended up coming up with a combined solution. Also, some of the code given in other answers is outdated as Postman has updated their API over the years, so this uses methods that work on 7.22.1.
pm.environment.set("basicJSON", '[{"key1":"value1","key2":"value2"},{"key1":"value1","key2":"value2"}]')
var encoded = encodedURIComponent(pm.environment.get("basicJSON"))
pm.environment.set("encodedJSON", encoded)
This solution requires that both basicJSON and encodedJSON exist as environment variables. But what was important for me was the ease of editing the object. I didn't want to have to decode/encode constantly to change values, and I didn't want to have to open the environment variables dialogue. Also, it's important to note the single-quotes around the object. Excluding them or using double-quotes would cause Postman to send something like "[object Object]" which is useless to an endpoint expecting actual JSON.
I had similar problem with braces { and } in query parameter.
By turning off the following setting it started working for me.
For the postman version 9.28.4 ==>
You can use 2 methods:
By selecting the part of the url in url bar -> right click -> EncodeURLComponent. (screenshot attached)
You can also use "pre-request script" tab of postman and write the script for the variable manually. (screenshot attached)
The problem with right-click => Encode URI Component is that it destroys the raw value of that parameter. You can use the following pre-request script to overcome this (which also works for cases where you have disabled that param):
// queryParam is of type https://www.postmanlabs.com/postman-collection/QueryParam.html
if ((queryParam = pm.request.url.query.one("name_of_your_query_param")) !== undefined
&& queryParam.disabled !== true) {
queryParam.value = encodeURIComponent(queryParam.value);
}
Click the Params button to open the data editor for URL parameters. When you add key-value pairs, Postman combines everything in the query string above. If your URL already has parameters - for example, if you are pasting a URL from some other source. Postman splits the URL into pairs automatically.
https://www.getpostman.com/docs/v6/postman/sending_api_requests/requests
POSTMAN's documentation on building requests in the section "sending parameters" is helpful here. You can encode path data by simply encoding the URL with a colon, listing the key name of the encoded element, and then a new section will appear below the query parameters allowing you to customize values and add a description, just as we do with query params. Here's an example of encoding the URL for a GET request:
https://somesite-api-endpoint/:record/get
And here's what the display looks like after you add path data. Any value you add in the path variables section will automagically update the URL with your data.

Django url patterns - how to get absolute url to the page?

i'm trying to get full path of the requested url in Django. I use a such url pattern:
('^', myawesomeview),
It works good for domain.com/hello, domain.com/hello/sdfsdfsd and even for domain.com/hello.php/sd""^some!bullshit.index.aspx (although, "^" is replaced with "%5E")
But when I try to use # in request (ex. http://127.0.0.1:8000/solid#url) it returns only "/sold". Is there any way to get the full path without ANY changes or replacements?
BTW, I'getting url with return HttpResponse(request.path)
Thanks in advance.
The part of URI separated by '#' sign is called a fragment identifier. Its sense is to be processed on client side only, and not to be passed to server. So if you really need this, you have to process it with JS, for example, and pass it as a usual parameter. Otherwise, this information will never be sent to Django.