multipart/mixed HTTP request in actionscript - actionscript-3

Is there an easy way to make an multipart/mixed HTTP request in as3?
I'm trying to reach LightSwitch server using OData protocol from flash. To send multiple commands at once you can combine them in one batch using HTTP request with "multipart/mixed" content type.
Here an example of HTTP requests with two commands:
Content-Type:multipart/mixed; boundary=batch
--batch
Content-Type: multipart/mixed; boundary=changeset
--changeset
Content-Type: application/http
Content-Transfer-Encoding: binary
PUT http://localhost:18065/ApplicationData.svc/Orders(3)/$links/User HTTP/1.1
Content-Type: application/json;odata=verbose
{"uri": "http://localhost:18065/ApplicationData.svc/Users(4)"}
--changeset
Content-Type: application/http
Content-Transfer-Encoding: binary
MERGE http://localhost:18065/ApplicationData.svc/Users(3) HTTP/1.1
Content-Type: application/json;odata=verbose
If-Match: W/"X'0000000000002715'"
{"Name": "User 3_"}
--changeset--
--batch--
Every part contains own HTTP headers and body. Response formatted the same way, with own http response code.
How to work with multiple HTTP requests in actionscript?

Related

Multipart request without image file

I want to call multipart service without uploading an image file. It only has input JSON data.
Is it possible to call multipart service with only JSON & file is optional?
it's absolutely possible to make a valid Multipart/form-data request without any files in, yes.
here is a HTTP Post request with multipart/form-data containing only the variable "foo" with the value "bar":
POST / HTTP/1.1
Host: 127.0.0.1:9999
User-Agent: curl/7.59.0
Accept: */*
Content-Length: 141
Content-Type: multipart/form-data; boundary=------------------------c6d530a086d1167c
--------------------------c6d530a086d1167c
Content-Disposition: form-data; name="foo"
bar
--------------------------c6d530a086d1167c--
generated with the curl command
curl -F "foo=bar" http://127.0.0.1:9999

Is a batch operation to save a set of navigation properties possible?

For example: Company has Employees.
By posting to odata.svc/Company(1)/Employees/$ref, I can save Company-Employee relation. Can I save multiple Company-Employee links this way using odata.svc/Company(1)/Employees/$ref/$batch ?
I tried but didnt work. If this is incorrect way, is there any other alternative ?
OData does support batching, I'm not sure what version of OData you are using but here is some documentation from v3 (v4 is the same) http://www.odata.org/documentation/odata-version-3-0/batch-processing/
In your example batch URL, you are adding $batch to the end of the URL that you are using, instead you have to POST to odata.svc/$batch with a request that contains details of all of the operations that you want to perform. For example, here is one of the request taken from that link:
POST /service/$batch HTTP/1.1 Host: host Content-Type:
multipart/mixed; boundary=batch_36522ad7-fc75-4b56-8c71-56071383e77b
--batch_36522ad7-fc75-4b56-8c71-56071383e77b Content-Type: multipart/mixed;
boundary=changeset_77162fcd-b8da-41ac-a9f8-9357efbbd621
Content-Length: ###
--changeset_77162fcd-b8da-41ac-a9f8-9357efbbd621 Content-Type: application/http Content-Transfer-Encoding: binary Content-ID: 1
POST /service/Customers HTTP/1.1 Host: host Content-Type:
application/atom+xml;type=entry Content-Length: ###
--changeset_77162fcd-b8da-41ac-a9f8-9357efbbd621 Content-Type: application/http Content-Transfer-Encoding: binary
POST $1/Orders HTTP/1.1 Host: host Content-Type:
application/atom+xml;type=entry Content-Length: ###
--changeset_77162fcd-b8da-41ac-a9f8-9357efbbd621--
--batch_36522ad7-fc75-4b56-8c71-56071383e77b--

XMLHttpRequest Header : Decode Binary Data Upon Server Arrival

I am building a multipart/form-data httprequest, using Javascript's XMLHttpRequest object. Here are the headers:
Accept: */*
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.5
Content-Length: 38539
Content-Type: multipart/form-data; charset=UTF-8; boundary=CONTENT_BOUNDARY
Cookie: __utma=111872281.167592067.1396133699.1396133699.1396133699.1; PHPSESSID=ea7cfjgi8e29v1bhnrj580vbc2
DNT: 1
Host: localhost:90
Referer: localhost:90/my_secret_referer/no_peeking.php
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0
I send the request to a PHP document.
In the following snippet one can observe a part of the request body. It details the transmission of binary data in base64 representing an image file that has been selected.
--CONTENT_BOUNDARY
Content-Disposition: form-data; name="images[]"; filename="airplane.jpg"
Content-Type: image/jpeg
Content-Transfer-Encoding: base64
.../9j/4AAQSkZJRgABAQEBLAEsAAD/2wBDABkRExYTEBkWFBYcGxkeJT4pJSIiJUw3Oi0...
This body part cooperates with the entire request to create a successful AJAX request to my server. When the information arrives at the server, I decode the information and do what I may with it.
The segment of this process that I am unsatisfied with is the manual decoding of the binary data. One is always able to receive uploaded images in the server as complete, legible image files when using jQuery's $.ajax() or Malsup's jQuery Form. I have looked into the format of the two mentioned requests and have found that they too send binary information using a multipart/form-data request. The one difference that I have found which I have not intentionally made is that my request is furnished with a
charset: utf8;
specification inside of the Content-Type after I send it.
Can I, and if I can, how can I tell the server to decode the binary information it receives?

How can I put Google Drive API files.list requests for different users in the same batch request?

Normally, one would use BatchHttpRequest to aggregate several requests, but what if I want to list files for two different users?
Nominally that requires an oauth2 token per-user. BatchHttpRequest either takes one http object (which handles the credentials) for the batch, or pulls it out of the first batched request encountered when .execute() is called.
Using oauth1 to sign each batched requests results in proper results (different feeds for each user). However, using oauth2 and manually constructing the payload results in identical feeds (matching the user in the first batched request):
POST /batch HTTP/1.1
Host: www.googleapis.com
Content-Type: multipart/mixed; boundary=blah
Content-Length: 572
accept-encoding: gzip, deflate
Cache-Control: no-cache
--blah
Content-Type: application/http
Content-Transfer-Encoding: binary
Content-ID: <b29c5de2-0db4-490b-b421-6a51b598bd22+1>
GET /drive/v2/changes HTTP/1.1
Authorization: Bearer ya29.UQAabvlG2hnRPyEAAADUm7vkDe_qg7L49R655IyxvgBnkN7_PEgE3IG7UnZ_ZEmJwUK_6fSV4kTHjNQIjTk
accept: application/json
--blah
Content-Type: application/http
Content-Transfer-Encoding: binary
Content-ID: <b29c5de2-0db4-490b-b421-6a51b598bd22+2>
GET /drive/v2/changes HTTP/1.1
Authorization: Bearer ya29.UQBu8f9W8S5E6RwAAAAPqCOiqoFW3QEFYkBvQGx36UVKNHeEhdZT8GPN-P74ng
accept: application/json
--blah
Since oauth1 works, it seems that this is at least theoretically possible, although I may be blocked by a google bug where the oauth2 token for the first request in the batch overrides any other authorizations included.
The google-provided client does not support this, but providing an Authorization header for each request will work. The example given should work assuming all of the authorization header values are correct.

Get http post request body in perl

Request header (from Firebug):
Accept application/json, text/plain, */*
Accept-Encoding gzip, deflate
Content-Type application/json;charset=utf-8
Request json:
{"key":"value"}
So how to get request body in perl?
What webserver?
Usually POST data is available by simply reading from STDIN.
If you are using the venerable CGI module (under mod_perl or not), you can get the body via:
$cgi->param('POSTDATA')
(if, as in this case, the content type isn't application/x-www-form-urlencoded or multipart/form-data)