python using post method for upload file - python

I have a problem with importing file to calendar over wcap protocol. In documentation http://docs.sun.com/source/816-6416-10/pr10WCAP.html#26125 is said that i should use POST method. I have trayed but with no positiv result. I have no idea hov to forced it to work with python

The wcap reference you provide contains example POST data.
Using the urllib2.urlopen() function with POST data is straightforward.
urllib2.urlopen(url[, data][, timeout])
Open the URL url, which can be either a string or a Request object.
data may be a string specifying additional data to send to the server, or None if no such data is needed. Currently HTTP requests are the only ones that use data; the HTTP request will be a POST instead of a GET when the data parameter is provided. data should be a buffer in the standard application/x-www-form-urlencoded format. The urllib.urlencode() function takes a mapping or sequence of 2-tuples and returns a string in this format.

Related

Understanding protobuff protocol

I'm just doing some reverse engineering exercise and have ran across application/x-protobuff protocol..
I am currently sniffing network calls from redfin using mitmproxy. I see a endpoint for a result, however the response is unstructured JSON formatted data with content type application/x-protobuff After doing a bit of research, I found out that protobuff uses a schema to map the data internally, and I am assuming the schema also sits in the client somewhere, called .proto file.
SS
To validate my assumption on what that screenshot tells is that
I can see there is a response header called X-ProtoBuf-Schema is that the the location where the schma would be located, the same schema I can use to decrypt the response data? How would I go on about reading that data in a more structured manner?
I am able to make a request using requests to that endpoint, just gives me protobuffers.
PS: This is what the JSON format looks like
https://pastebin.com/LY51X9KZ
"and I am assuming the schema also sits in the client somewhere, called .proto file." - I wouldn't assume that at all; the client, once built, doesn't need the .proto - the generated code is used instead of any explicit schema. If a site is publishing a schema, it is probably a serialized FileDescriptorSet from google/protobuf/descriptor.proto, which contains the intent of the .proto, but as data.

How can I build HttpResponse with multipart ContentType

I have purpose to send in single response several data types: plain/text (or json) and image as bytes. And I see, how I can set contenttype for my response. So and I can set some multipart type:
response = HttpResponse(my_data, content_type='multipart/alternative')
But how I understand, this way leads to difficulties: I need set also content type header for each part of the response. But I cann't find in docs, how can I do that. And is it possible for django?
Thanks for your ideas
This content type is for mail purposes. It will not be rendered correctly by web browsers.
Check this question: Browser support of multipart responses

Get Headers/Cookies from Bottle API request

I was making an API call to a Bottle service and was passing headers in the call using Python's request Library.
requests.get('http://localhost/API/call', headers={"cat":"tax"})
I wanted to get the custom headers passed in the function that gets called through the API call.
Using bottle.request.headers I get the following data:
Now, the custom header I passed is present in the environ dictionary with key/value 'HTTP_CAT':tax.
Same thing for cookies. Cookie data can be retrieved using bottle.request.cookies
How can I filter out only the custom header that I am passing in the request?
I'm not sure exactly what you mean by "filter," but the typical way to retrieve request headers from Bottle is with get_header:
cat_value = request.get_header('cat')
Bottle also has a specific API for retrieving individual cookies. Perhaps there's a good reason you're going down to the raw environ, but if not, then you should be using these built-in methods.
PS, you may also want to prefix your custom headers with "X-", e.g. X-Cat.

Disabling Angular $http.get URL encoding for query parameters

I am using Flask-Restless to create my API, which requires queries parameters to be formatted using a list of filter objects. A valid query follows this format:
/api/person?q={"filters":[{"name":"firstName","op":"like","val":"Mike"}]}
(See Flask-Restless query docs here.)
When I use Angular's $http.get to pass the query params, they are encoded in the URL and break the query:
GET /api/person?q=%7B%22filters%22:%7B%22name%22:%22firstName%22,%22op%22:%22like%22,%22val%22:%22Mike%22%7D%7D HTTP/1.1"
Is it possible to disable encoding for all or some of the parameters?
UPDATED ANSWER:
Angular will by default stringify a params value if it is an object.
Thus, it will make all necessary work for you, just by passing the queryObject:
var queryObject = {filters: [...]};
$http.get('...', {params: {q: queryObject}});
What you see is the stringified object encoded as a URI component.
That is probably not done by Angular, but by your browser itself.
(E.g. try making a simple XHR to the same URL and insect the request in the Network panel of the DevTools.)
I am not familiar with Flask, but (if it doesn't automatically decode the query param) it should have a method to do so manually.

How to pass body with Google APIs Client Library?

I use Google APIs Client Library for Python to work with Fusion Tables API. importRows method here requires to provide the data in the body. How should I do it?
response = service.table().importRows(tableId=TABLE_ID, body='zzz,yyy').execute()
returns the error - Got an unexpected keyword argument "body".
There's a slight subtlety here -- the body of the request should be the Table resource, if you want to update it; the contents (in this case, the rows) should actually be passed as a media upload.
In the python client, this means you want to pass something in to the media_body argument, not body. You can't just pass a literal string -- you need to wrap the data in either a MediaFileUpload or MediaInMemoryUpload. (For the case here, you want the latter, but if you've got a file with rows on disk, you want the former.)

Categories