Get original bytes from request - dart

How I get original bytes from request? Calling request.body.asBytes() I get this message:
asBytes() expected list of bytes, instead got List<_InternalLinkedHashMap<String, dynamic>>
I saw that HTTPRequestBody has the property retainOriginalBytes to use in this case, but where I set it?
Thanks!

Whatever endpoint you're hitting with your request is returning a Map in its body, not a list of Bytes.
I'm not sure if you can control the contents of what that endpoint is returning but if you can that would be the place to change it.
Check out the BytesBuilder class. Also, read the Aqueduct docs for Request and Response Objects. Hopefully this gets you on the right path!

You're on the right track; this will work correctly once you've set retainOriginalBytes to true. This must be done before the body is decoded.
In an HTTPController, the request body is decoded before your method that handles the request is called. Just before the decoding, HTTPController calls its willDecodeRequestBody() method. This method does nothing by default, but you can override it to set retainOriginalBytes:
#override
void willDecodeRequestBody(HTTPRequestBody body) {
body.retainOriginalBytes = true;
}
Here is an example of an application that does this.

Related

Does assigning a post request to a variable read all of its Reponse contents into memory?

Let's say we gave a generic POST request with Python's requests.
req = requests.post('http://someapi.someservice.com', files=files)
req will be a Response object. In my case, the .content of the response can be very, very large so I do not wish to read it all into memory. Luckily, requests provides an iterator .iter_content that allows iteration over the contents. My question is, though, does req contain all contents of the response already (and as such everything is already read into memory), or does calling .content and as such .iter_content initiate a download which really fetches the content? This is important, because if assigning the POST request to a variable already reads the Response's content into memory, then of course using .iter_content makes no difference.
You will need to set the stream parameter to True in your request in order to avoid downloading the entire content of the response into the response object:
req = requests.post('http://someapi.someservice.com', files=files, stream=True)
Excerpt from the documentation of Body Content Workflow:
By default, when you make a request, the body of the response is
downloaded immediately. You can override this behaviour and defer
downloading the response body until you access the Response.content
attribute with the stream parameter... You can further control the workflow by use of the Response.iter_content() and Response.iter_lines() methods.

How to test POST method with Postman and x-www-form-urlencoded Body?

I'm at the final step of making my first JS Web App using Pug, where I need to test that it supports POST method, which takes in a string should return a JSON object with 2 key.value pairs that represent original string and its length.
I'm instructed to use Postman and x-www-form-urlencoded to test my App, but I don't know how to test the POST method using urlencoded body.
Can someone tell me what I should fill in for KEY and VALUE on Postman, under x-www-form-urlencoded?
//POST method in route file
router.post('/', (req, res, next) => {
res.render('ps3post', {string: req.body.string, stringLength: req.body.string.length});
});
block content
h1 POST method, page rendered by PUG
p= string
p= stringLength
Okay I figured it out.. It seems that KEY needs to be type (string in my example), and VALUE needs to be what I want the actually value to be.

Why does the iOS UIWebView loadRequest method not return a value?

In iOS, the Xcode/Swift method to load an HTTP request is void, and returns no value.
Why not return the response code? For example, on a successful page request, return 200; on a not found, return 404.
I know that the status variable can be pulled from the header in the WebViewDidFinishLoad Method (ref link below), but it seems that a return code directly from the loadRequest method would make sense.
EDIT: Updated clip from documentation
Get the http response code from every UIWebView request
Ah, so you meant loadRequest? As you can see from the docs you've quoted, it's asynchronous, so it will almost certainly return before the request has completed, so it can't return the response code as the response hasn't happened yet.

RestResponse is null on POST request using RestSharp

I am making a POST request with RestSharp (on windows phone 7.1 client). I sent string to a service in a request body. Looks like the service is successfully called and it returns proper value (integer), however response object is null:
client.ExecuteAsync<T>(request, (response) => {
data = response.Data; // response is null in debugger
});
I cant understand why is that so.
<T> isn't a valid value for that call. I'm not sure that would even build there unless you've wrapped it in a generic method.
Also, is the response coming back as plain text? What's the Content-Type returned? Most likely, you should just use ExecuteAsync(request, callback) without the generic parameter and grab the data out of response.Content which is a string of the response body. response.Data is for the automatically deserialized XML or JSON (or custom) response if you use the generic method overload that specifies a type to deserialize to.
This seems to be an ongoing issue with RestSharp asynchronous calls - for HTTP transport errors ErrorException object is useless (returns null). Check the StatusCode property if it returns with anything but HttpStatusCode.OK. StatusDescription is not extremely useful either as it doesn't match complete status message from server response payload.

HTTP Response changed?

i have an HttpWebRequest that do 'POST' to a web server and get an HTML page In response.
I've been asked how is the best practice to know that the response i got has been changed or not?
I can't relay on the web server headers, they don't have to be.
this will increase performance in a way that i wont be need to parse the response over again and will go to the next request in a half of a sec or so.
thank you in advanced
You can tell your webserver about last modification date. see here. If you can't rely on that you have to parse your response anyway. You could do that quickly using md5. So you "md5" your current response and compare it with the previous.
You shouldn't be attempting to rely on the headers for a POST request, as it shouldn't emit any caching headers anyway.
What you need to do instead is perform a hash/checksum (this can either be CRC(32) for absolute performance or a "real" hash such as md5) on the returned content (this means, everything underneath the \r\n\r\n in the headers) and do a comparison that way.
It should be good enough to store the last request's checksum/hash and compare against that.
For example (psuedo):
int lastChecksum = 0;
bool hasChanged() {
performWebRequest();
string content = stripHeaders();
int checksum = crc32string(content);
if(checksum != lastChecksum) {
lastChecksum = checksum;
return true;
}
return false;
}

Resources