Getting progress on POST using HTTPService in Flex/AS3 - apache-flex

I'm using HTTPService with a POST operation to submit a Base64 encoded file (taken from bitmap data within the app) but I could really do with getting some idea of the progress of the POST operation (e.g. like the FileReference.upload()).
I don't think this is possible, but it would be awesome if it is (via any means, I'm willing to change my setup to get this).

Do not use HTTPService. Use URLRequest, URLLoader, and URLVariables.
If your using an HTTPService tag, get ride of it and replace it with a Script tag filled with something like ...
private function forYou() : void{
var req : URLRequest = new URLRequest("PUT YOUR URL HERE")
var loader : URLLoader = new URLLoader();
var params : URLVariables = new URLVariables();
params.WHATEVER = WHATEVER YOU WANT IT TO BE;
req.data = params;
req.method = URLRequestMethod.POST;
loader.addEventListener(ProgressEvent.PROGRESS, YOUR LISTENER FUNCTION NAME);
loader.load(req);
}
Assign this function name to the creationComplete attribute of the root tag.
If your not using an HTTPService tag, just get ride of the HTTPService object in your actionscript and use the above code.

This worked well for me to consume a REST web service:
http://code.google.com/p/as3httpclient/wiki/Links
Example

This isn't possible with HTTPService. Its only events are result, fault, and invoke (other than the non-relevant inherited events of activate and deactivate).
To get progress information on an upload process, the server would have to provide the information, which would require a means of communication between the server and the client during the operation, which isn't there for a regular HTTP POST operation.
One option might be to create an object on the server that would be instantiated whenever the server began receiving POST data from your client. It would then keep track of the progress and expose that data to the rest of your server-side application. Your client could then initiate a polling system that would request the value of that particular variable.
Seems like kind of a far-fetched option, though...

Related

Get the JSON that was used to call our ASP.NET REST Web API

We often have the case that clients call our ASP.NET API with an invalid JSON. The mistakes can be anywhere from wrong fields to wrong formatting.
Yes, you could argue that this is the clients problem, but this will not make my life easier.
Is there a way to get to whatever the client sent us, if the controller throws an Exception?
This code snippet will allow you to see the raw content of the request body:
using (var reader = new System.IO.StreamReader(System.Web.HttpContext.Current.Request.InputStream))
{
var content = reader.ReadToEnd(); // raw content of request body
}

could not be mapped to a JSON object in Logansquare

i am trying to fetch data from Realm and sending it to server using retrofit and for parsing and serializing i am using LoganSquare
client = new Retrofit.Builder()
.baseUrl(REST_ENDPOINT)
.client(okHttpClient)
.addConverterFactory(LoganSquareConverterFactory.create())
.build();
this is how i am accessing record
Appointment appointments = DB.getInstance(mContext).selectNotSyncAppointmentsData();
RestApi.AppointmentsDataApi service = getAppointmentsDataApi();
Call<APResponse> call = service.createUpdateAppointmentsData(appointments);
i am getting following error
createUpdateAppointmentData : onFailure Class io.realm.AppointmentRealmProxy could not be mapped to a JSON object. Perhaps it hasn't been annotated with #JsonObject?
I am not familiar with how LoganSquare works, but be aware that Realm works with something called proxy classes that extend all your model classes. You can recognise these as they are called <ModelClass>RealmProxy. It looks like you somehow have to find a way to configure LoganSquare to recognise these sub-classes.
Alternatively you can use realm.copyFromRealm(appointments). That will give you an in-memory copy of all the data which are of the correct class.

How to create an endpoint that accepts any content type in the request body using ASP.Net Web API

I'd like to create a generic API endpoint that a client can post text or file data to, where we won't know the content/media type of the data. It seems the framework requires a content formatter to be specified for any content-type passed in the HTTP header, or it throws an error. I don't want to have to define a formatter for every possible media type we might accept since we don't know yet what all they could include.
Is there a way to define an endpoint with a generic media type formatter, or not specify one at all? It doesn't seem to mind if I use a generic Object as my method parameter, but the framework keeps getting hung up on not being able to handle the media type without a formatter.
We don't actually need to be able to process this data, just store it (for something like a messaging system).
On a side note, I'd rather receive this data as the raw content of the request body and not use a multipart form request, but if it would make more sense to do it that way, it might be an option.
Or if you want to go even more low level than Youssef's suggestion, you can do..
public Task<HttpResponseMessage> Post(HttpRequestMessage request) {
var stream = await request.Content.ReadAsStreamAsync();
return new HttpResponseMessage(HttpStatusCode.Ok) { RequestMessage = request } ;
}
You can bypass formatters entirely by reading the content yourself. Here's an example:
public async Task Post()
{
string content = await Request.Content.ReadAsStringAsync();
// Store away the content
}
This doesn't require you to use or define any formatters at all.

flex 3: possible to send array in URLRequest?

In efforts to allow users to save their progress in my application, I've decided to allow them to save. In order to do this, I'd like to create an array with all the necessary information, and send that information to a coldfusion (.cfm) file and process the information from that page. Is it possible to send an array instead of a bunch of url variables? It is possible (and quite probable) that users would exceed the query string length of most browsers.
Yes, just use a post method instead of get. In ColdFusion this will come through the form scope instead of the url scope.
var request:URLRequest = new URLRequest(your-cf-page);
request.data = yourURLVariablesObject;
request.method = URLRequestMethod.POST //this is the important part
urlLoader.load(request);
Put your info in the URLVariables like you usually do...
yourURLVariablesObject.whatever
will become
#form.whatever#
on CF
Just a note... if you really want to make this work well, I'd consider using AMF and an RemoteObject. ColdFusion has the advantage of being able to directly talk to Flex via AMF.
var yourService:RemoteObject = new RemoteObject("ColdFusion");
yourService.source = "yourCFFiles.yourCFC";
Now you can call any method in yourCFC

Make an ASP.NET Web service output an RSS Feed

I have been writing some Web services to be used by a few different client apps and i was trying to write a web service method that simply outputs an RSS XML Feed.
I can create the XML using an XmlTextWriter Object
Then i have tryed outputing to the Response (like i have done in the past when its an aspx page) but this only works it the return type is void (and still doesnt seem to output properly)
Then i tryed making the return type a string and using a StringWriter to output the xml from the XmlTextWriter but the output is then wrapped in a tag.
How can i do this?
Obviously create the interfaces and rest of the WCF service as normal.
Mark the class with the following attribute
[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
And then this function
public Stream GetRSS()
{
string output;
//output = some_text;
MemoryStream ms = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(output));
WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
return ms;
}
I have some code for this, but it's more than will fit well in an SO post (about 1000 lines). It's really not that hard; the schema is simple enough you can do it yourself, but you don't have to: there are several components you can just plug in to create the xml for you.
You should see this question:
ASP.Net RSS feed
If you must use ASMX, then you can return an XmlDocument. Build the feed XML however you like, but then return the XmlDocument from your web method.

Resources