Getting HTTP 400 error while uploading xml file

Hi,

I am using REST API with Flash Professional IDE.
I can upload all other file type without any error but uploading xml file is successful only 2-3 time out of 5 times.
I am using AS3 command file.upload(). Upload progress shows uploading till 100% but file is not uploaded on the server. I also tried setting URL request to POST and content-type to multipart/form-data but problem reamains. Please see below code.



private static var filesUrl:String = Backendless.API_URL + Backendless.VER + '/files/'
public static var headers:Array = [
			new URLRequestHeader(Backendless.HDR_APP_ID, Backendless.APP_ID),
			new URLRequestHeader(Backendless.HDR_SCR_KEY, Backendless.APP_KEY),
			//new URLRequestHeader(Backendless.HDR_CONT_KEY, Backendless.CONT_JSON),
			new URLRequestHeader(Backendless.HDR_APP_TYPE, Backendless.APP_TYPE)
		]
public static function FileUpload(file:File, location:String, success:Function, progress:Function, error:Function):void{
			var url: String = filesUrl + location + '/' + file.name			
			
			file.addEventListener(Event.COMPLETE, function (e: Event): void {
				file.removeEventListener(Event.COMPLETE, arguments.callee)
				file.removeEventListener(IOErrorEvent.IO_ERROR, arguments.callee)
				file.removeEventListener(ProgressEvent.PROGRESS, arguments.callee)
				success()
				trace("file ", file.name, "uploaded successfully.")
			})
			file.addEventListener(IOErrorEvent.IO_ERROR, function (e: IOErrorEvent): void {
				file.removeEventListener(Event.COMPLETE, arguments.callee)
				file.removeEventListener(IOErrorEvent.IO_ERROR, arguments.callee)
				file.removeEventListener(ProgressEvent.PROGRESS, arguments.callee)
				error(e.text)
				trace("error uploading file: ", e.text)
			})
			file.addEventListener(ProgressEvent.PROGRESS, function(e:ProgressEvent):void{
				var perc:int = int((e.bytesLoaded/e.bytesTotal) * 100)					
				progress(perc, e.bytesLoaded*0.001, e.bytesTotal*0.001)
			})
			file.addEventListener(HTTPStatusEvent.HTTP_RESPONSE_STATUS, function(e:HTTPStatusEvent):void{
				trace('HTTP Status while uploading file:', e.status)
				for each(var r:URLRequestHeader in e.responseHeaders){
					//trace("response header:: ", r.name, " : ", r.value)
				}
			})
			
			var req: URLRequest = new URLRequest(url)
			req.requestHeaders = Backendless.headers
			//req.contentType = Backendless.CONT_DATA
			//req.method = URLRequestMethod.POST


			try {
				file.upload(req)
			} catch (error: Error) {
				trace("error uploading ", file.name)
			}
		}

My app id: B25A7713-84FC-1934-FF8B-663AD7320E00Uploading is mostly like hit or miss. Please help regarding this issue.

Thanks,
Tushar

Tushar,

Please use Charles Proxy to see what goes on the wire and why the requests fail.

Regards,
Mark

Hi Mark,

Thanks for quick response.
While uploading AppData.xml file I got response in Charles “File Already Exists”.
However, according to above code,

  1. First I rename existing file AppData.xml on server to AppData.xml.bak
  2. I upload new AppData.xml file to server
  3. I delete renamed file AppData.xml.bak on server.
    Somehow, while uploading AppData.xml.bak is still ‘seen’ as AppData.xml and as a result above error comes.
    Now, I rename file to temp.xml instead of adding a suffix .bak, and uploading of AppData.xml is fine as expected.

Thanks,
Tushar

Hi Mark,

Sorry, on further testing it still gives the same response error on Charles:
{“code”:6003,“message”:“Unable to upload the file: file already exists”}

I think same issue came while downloading xml file (http://support.backendless.com/t/getting-http-error-400-while-downloading-an-xml-file-through-rest) which was resolved from your side by Kate.

Please let me know what can be done.

Thanks,
Tushar

Try to avoid names with “double” extensions. For example do not rename to “.xml.bak”.

Yes, that’s why I renamed to temp.xml and file was uploading successfully 10-12 times when I tested until around 7pm IST. Then again I started getting same upload error as mentioned above.

Hi,

This is really frustrating. Sometimes I get this errors too frequently sometime not at all.
But file upload is not consistence at all.
Please see attached snapshots of Charles, one of the successful upload and one of the error.
I have to resolve the upload issue ASAP. Please guide.

Thanks,
Tushar

What do you expect us to do if a file already exists? Are you saying the uploaded file does not exist at the time of the upload?

  1. Yes. Since traditional upload method does not provide overwrite, existing file is first deleted then a new file of the same name is uploaded. But the problem is, error comes only half of the times and other times upload is successful. If I get error every time then I can debug my code.

  2. There are times I also get error 1132 while updating or adding an object to a table. On examining the request code in Charles, request body of JSON is fine still I get error 1132 syntax error. I get this error once every 7-8 time I call update object API.

Thanks
Tushar

If you make the upload call immediately after the call to delete a file, then all file repositories may not be synchronized yet. This would explain why the error sometimes shows up. You need to wait at least a few seconds before uploading after deletion.
There is “overwrite” argument in the REST implementation of File upload:
https://backendless.com/documentation/files/rest/files_file_upload.htm
You’re welcome to make a change in the SDK to support that as well.

As for error 1132, in the documentation it is defined as “Unable to save entity. Related table %s does not exist.”. I am not sure what mean when you refer to a syntax error.

Mark

Hi Mark,

Thanks for the guidelines regarding point 1.
Will implement this and check.
For point 2, I am sorry I got confused with syntax error with error code. But the code is 1132. Can you put light on this, why I might be getting this error while updating or adding object in table? Of course table exists with schema definition during update call.

Your help is much appreciated.
Tushar

BTW, in File upload method in rest, I didn’t see overwrite argument.
It is there in File Save with byte array method.

Hi Mark,

I just saw the document for File Upload method in REST. Overwrite property IS there. I think this must be updated lately.
Apologies for my previous comment.
I will definitely try this now.

Thanks,
Tushar

Hi Mark,

I tested 7-8 times File Upload method with overwrite and it works fine without any error.
Thanks a ton!
But I am really wondering how it went unnoticed by me because I have gone through documentation so many times. Or maybe this was updated just yesterday.

Thanks
Tushar

Yes, the docs have been updated recently - I believe, some time earlier this week.