How to GET the query parameters in Django-Tastypie - django

I am building a REST API for my application using Tastypie.
I've gone through this thread , but it didnt worked.
Actually, I want to pass a parameter to this method through the url (something like http://127.0.0.1:8000/api/v1/shipments/140119758884542/pptl_id/?DSP=1 ) and then perform a query based on this parameter.
The problem is that I can't get its parameter !
When printing the bundle variable, I see this :
<Bundle for obj: 'Shipment object' and with data: '{}'>
When printing kwargs variabl, I see this
{'pk': u'140119758884542/pptl_id'}
How do I get the query parameters?
Thanks for your help

Django's request object is kept in the bundle under the property named request.
You can use:
bundle.request.GET
in order to access the query parameters.
See documentation on the request document here

Related

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]

What do these arguments mean in swagger_auto_schema (Django)?

The project uses a swagger.
There is the following code.
#swagger_auto_schema(
manual_parameters=[
Parameter('download', IN_QUERY,
'Set `Content-Disposition=attachment` to make browser to download file'
'instead of showing it.',
type='bool'),
Parameter('share_id', IN_PATH, type='uuid')
],
security=[],
responses={'400': 'Validation Error (e.g. base64 is wrong)',
'200': VideoSerializer}
)
Please explain what each argument is responsible for.
I read the documentation, but understood little ...
Particularly interested in '200': VideoSerializer
responses
The responses argument is a dictionary of possible responses that this endpoint can return.
400 and 200 are HTTP response codes, Bad Request and OK respectively.
In this case, this means that this endpoint can generate two types of responses:
Bad request which will also return (as described) a Validation Error which means that something in the request was incorrect, which means it could not be handled correctly.
OK, which means the request is correct, and everything was handled correctly. VideoSerializer means that a response will be given with accordance to the structure of the VideoSerializer, which defines a collection of fields.
The other two arguments:
manual_parameters
This a custom list of parameters that can be added to the request to customize the response.
In this case, two parameters are defined:
download : A query parameter of type bool. Query parameters are passed like this : `example.com?query_parameter=true
share_id, a path parameter of type 'uuid'. Path parameters are passed like this : example.com/path_parameter
security
A list of security schemes that the request must adhere to. Used for instance for Basic authentication.

How do you see what an http request contains

I'm using a django framework for my assignment. I need to view what an http request object contains. print(request) is obviously not working. If I can see the object like in a json structure it would've been a huge help to understand what it would look like and what are the values it contains.
The best way to see what request contains (and more to that - any other object in python) is to use simple:
print(request.__dict__)
Depending on what type of request is GET or POST. You can find print(request.GET) or in print(request.POST) which is of <type 'dict'> and you can access data simply by request.GET.get("somedata") will return None if not found
Note: If you want to see attributes of request object you can do print(dir(request)) and you can do that on any object.
For more quick help you can do help(request) in python shell.

How do I format syntax on an API call to page through the results?

I am using the SeatGeek API to pull some data for sporting events. The results of the call I get show 145,000+ results returned in the metadata:
meta : {u'per_page': 10, u'total': 145419, u'geolocation': None, u'page': >1, u'took': 7}
in_hand : {}
When I look at the "Pagination" section of the API documentation, it gives examples of how to format the syntax in order to see more results per page, or to return a different page than the first one:
$curl 'https://api.seatgeek.com/2/venues?per_page=25&page=3'
But I can't get it to call successfully with the authorization I've been given from SeatGeek:
$curl https://api.seatgeek.com/2/events?client_id=MYCLIENTID&client_secret=MYCLIENTSECRET
I have tried using curl and python requests. I have tried putting the pagination syntax first and the "clientid/secret" second. I've tried putting the "clientid/secret" first. I've emailed the mods for the API to no avail.
This is the first API call I've ever made, so I'm sure there's some tiny formatting error I'm making.
The calls return successfully when I'm not using pagination. I just can't view more than one page of the results, and I'd like to view everything.
Thank you.
For sake of completeness and closure, I'm posting this as an answer:
OP used ? instead of & between the URL parameters. The correct URL is https://api.seatgeek.com/2/events?client_id=<CLIENT_ID>&clien‌​t_secret=<CLIENT_SECRET>&per_page=25&page‌​=3.

Retrieving all querystring variables from request object

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.