ASIFormDataRequest Uploading file - cocoa

Good Day!
i am using ASIFormDataRequest to upload file, now everthing works but the upload of file does not work. it does nothing, but other than that it works fine, i dont understand the reason, can you please help me out. here is my calling code.
[self setRequest:[ASIFormDataRequest requestWithURL:[NSURL URLWithString:#"http://localhost:8888/registerUserASI.php"]]];
[request setPostValue:strDeviceID forKey:#"userDeviceId"];
[request setPostValue:strCurrentDate forKey:#"userDateTime"];
NSData *data = UIImagePNGRepresentation(image.image);
[request setData:[[data base64Encoding] dataUsingEncoding:NSUTF8StringEncoding] forKey:#"userfile"];
btw i have also tried the SetData and AddData methods of this request as well. but same results.
The Php code is Working fine with any file upload but not this one, Can somebody help me out.
It might be php code issue that i am not properly handling for this specific request. but with other methods the uploading is working, but i want to work with this.
thanks

you say it does nothing. Did you start your request:
[request startSynchronous];
or
[request startAsynchronous];
I mostly use the async way, cause it does not freeze any UI. Then you have also to set the delegate of the request, so you also receive callbacks on success or fail of the request.

I was not converting space to its %20 just because of that it was not working.

Related

BlobClient - What is the recommended way to check if my upload was successfull?

The Azure blob client's Upload method returns a Response<BlobContentInfo>.
If the upload throws an exception, than something is wrong, that's clear.
Failure cases, like bad connection string or no internet connection does throw an exception. And it retries by default.
But if I do get a Response back, can still something be wrong?
On the BlobContentInfo I see no indicator of any error.
The Response has a GetRawResponse method, and there is a IsError bool flag on that, and a Status for HTTP status code. But no specific description if something goes wrong.
Maybe check for IsError, and try to log the response, if it's true, and assume it did work, if there is no error. And check the ContentHash I guess.
But if I do get a Response back, can still something be wrong?
No. It would mean what you were trying to upload got uploaded successfully.
If your upload fails, the method will raise an exception of type RequestFailedException.

Possible causes may include a bad/expired/self-signed certificate

I used self-signed certificate to request https buy ASIFormDataRequest, but But there may be a problem:
A connection failure occurred: SSL problem (Possible causes may include a bad/expired/self-signed certificate, clock set to wrong date)
__block ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:url]];
[request setValidatesSecureCertificate:NO];
[request startAsynchronous];
This really solved my problem. But it did find other problems.
look the pictures:
I can see the HTTPS response clear-text, Is security?

AFNetworking 2.0: NSURLSessionDownloadTask is cancelled when received 302 to AWS S3

I'm using a simple example from the README file (from AFNetworking 2.0 page) to download an image. All works fine for the images served directly from my web-sever.
But for those located on the S3 the web-server returns 302 Redirect the following error:
Error Domain=NSURLErrorDomain Code=-999 "cancelled"
When I try to download the image (using the same code) directly from the S3 (omitting the redirection) I receive the same error.
I've checked the availability of the actual image with wget. The image is publicly available (https://youguide.s3.amazonaws.com/icons/user_96x96.png).
Anyone experience the similar problem?
I was having the same problem with S3 and seem to have resolved it. AFNetworking was returning a value in the authentication challenge handling logic that was canceling the request.
By telling the AFURLSessionManager how to handle authentication challenges I've fixed the problem.
[manager setSessionDidReceiveAuthenticationChallengeBlock:^NSURLSessionAuthChallengeDisposition (NSURLSession *session, NSURLAuthenticationChallenge *challenge, NSURLCredential * __autoreleasing *credential) {
return NSURLSessionAuthChallengePerformDefaultHandling;
}];

Grails Canoo Webtest plugin: invoke() fails to send JSON data in POST request

I'm evaluating Canoo Webtest for automated integration/functional testing as a Grails plugin.
I have a REST app which I'm attempting to test, but Canoo Webtest doesn't seem to properly send the JSON data in POST request. My test code is like below
invoke( description:"Add a product to shopping cart",
url:'shoppingCart/add', method:'POST',
content:'{"class":"shop.service.Product", "name":"A product", "description":"Manufactured by X", "price":99.9}'
//contentFile: '../product.json'
)
The request body is empty no matter whether I use contentFile approach or inline the data as content attribute. The test report shows the data as being sent correctly, but error page shows an error stating 'JSONException: Missing value. at character 0 of '. JSON data as a response of GET request is coming back fine.
I have tested the same functionality with curl and it works perfectly fine. Is there something I'm missing in the Canoo Webtest setup?
Thanks.
EDIT: I'm using Grails 1.3.7 in case that makes any difference
OK. I started to just experiment with the invoke() and seems that setting attribute soapAction value to true does the trick.
I have no clue why this works. The documentation of invoke() for the attribute says
soapAction
Required? no
If the HTTP method is POST and is in fact a SOAP POST request,
this allows the SOAP Action header to be set. Ignored for GETs.
Apparently it sets some needed request header. Haven't checked which one.
Thank you for reading :)
Cannot yet mark this answer as the correct one, but will do so when it's possible.

NETWORK_ERROR: XMLHttpRequest Exception 101

I am getting this Error
NETWORK_ERROR: XMLHttpRequest Exception 101
when trying to get XML content from one site.
Here is my code:
var xmlhttp;
if(window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
}
if (xmlhttp==null) {
alert ("Your browser does not support XMLHTTP!");
return;
}
xmlhttp.onReadyStateChange=function() {
if(xmlhttp.readyState==4) {
var value =xmlhttp.responseXML;
alert(value);
}
}
xmlhttp.open("GET",url,false);
xmlhttp.send();
//alert(xmlhttp.responseXML);
}
xmlhttp.open("GET",url,false);
xmlhttp.send(null);
Does any one have a solution?
If the url you provide is located externally to your server, and the server has not allowed you to send requests, you have permission problems. You cannot access data from another server with a XMLHttpRequest, without the server explicitly allowing you to do so.
Update: Realizing this is now visible as an answer on Google, I tried to find some documentation on this error. That was surprisingly hard.
This article though, has some background info and steps to resolve. Specifically, it mentions this error here:
As long as the server is configured to allow requests from your web application's origin, XMLHttpRequest will work. Otherwise, an INVALID_ACCESS_ERR exception is thrown
An interpretation of INVALID_ACCESS_ERR seems to be what we're looking at here.
To solve this, the server that receives the request, must be configured to allow the origin. This is described in more details at Mozilla.
The restriction that you cannot access data from another server with a XMLHttpRequest can apply even if the url just implies a remote server.
So:
url = "http://www.myserver.com/webpage.html"
may fail,
but:
url = "/webpage.html"
succeed - even if the request is being made from www.myserver.com
Request aborted because it was cached or previously requested? It seems the XMLHttpRequest Exception 101 error can be thrown for several reasons. I've found that it occurs when I send an XMLHttpRequest with the same URL more than one time. (Changing the URL by appending a cache defeating nonsense string to the end of the URL allows the request to be repeated. -- I wasn't intending to repeat the request, but events in the program caused it to happen and resulted in this exception).
Not returning the correct responseText or responseXML in the event of a repeated request is a bug (probably webKit).
When this exception occurred, I did get an onload event with readyState==4 and the request object state=0 and responseText=="" and responseXML==null. This was a cross domain request, which the server permits.
This was on an Android 2.3.5 system which uses webKit/533.1
Anyone have documentation on what the exception is supposed to mean?
Something like this happened with me when I returned incorrect XML (I put an attribute in the root node). In case this helps anyone.
xmlhttp.open("GET",url, true);
set the async part to true
I found a very nice article with 2 diferent solutions.
The first one implementing jQuery and JSONP, explaining how simple it is.
The second approach, it's redirecting trough a PHP call. Very simple and very nice.
http://mayten.com.ar/blog/42-ajax-cross-domain
Another modern method of solving this problem is Cross Origin Ressource Sharing.
HTML5 offers this feature. You can "wrap" your XMLhttp request in this CORS_request and
if the target browser supports this feature, you can use it and wont have no problems.
EDIT:
Additionaly i have to add that there are many reasons which can cause this Issue.
Not only a Cross Domain Restriction but also simply wrong Settings in your WEB.CONFIG of your Webservice.
Example IIS(.NET):
To enable HTTP access from external sources ( in my case a compiled Phonegap app with CORS request ) you have to add this to your WEB.CONFIG
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
Another scenario:
I got two webservices running... One on Port 80 and one on Port 90. This also gave me an XML HTTP Request Error. I even dont know why :). Nevertheless i think this can help many not well experienced readers.

Resources