Getting HTTP error 400 while downloading an xml file through REST

Hi,

I am able to download jpg, png, pdf and mp4 files through REST API but I get HTTP error 400 while downloading an xml file.
I am using ActionScript in Flash Professional CC2015.
Sometimes I am able to download, most of the times not.

Below is code I am using:

public static var headers:Array = [
new URLRequestHeader(‘application-id’, ‘my App Id’),
new URLRequestHeader(‘secret-key’, ‘my App Secret Key’),
new URLRequestHeader(‘Content-key’, ‘application/json’),
new URLRequestHeader(‘application-type’, ‘REST’)
]

public static function FileDownload(location: String, filename: String, success: Function, progress: Function = null, error: Function = null): URLLoader {
var url: String
if(location != ‘’){
url = filesSecureUrl + location + ‘/’ + filename;
}else{
url = filesSecureUrl + filename;
}
trace(“downloading url:”, url)
var req: URLRequest = new URLRequest(url)

		req.requestHeaders = Backendless.headers
		req.requestHeaders.push(new URLRequestHeader('user-token', BackendUser.user_token))
		req.method = URLRequestMethod.GET
		
		var loader: URLLoader = new URLLoader();			
		var a:Array = filename.split('.')
		var ext:String = String(a[a.length-1]).toLowerCase()
		
		if(ext == 'txt' || ext == 'xml'){
			trace('data type is text or xml')
			loader.dataFormat = URLLoaderDataFormat.TEXT
		}else{
			trace('data type is binary')
			loader.dataFormat = URLLoaderDataFormat.BINARY
		}
		
		var loaded_fn: Function = function (e: Event): void {
			loader.removeEventListener(Event.COMPLETE, loaded_fn)
			loader.removeEventListener(IOErrorEvent.IO_ERROR, onIOError_fn)
			loader.removeEventListener(ProgressEvent.PROGRESS, progress_fn)


			success(loader.data)
		}
		var progress_fn: Function = function (e: ProgressEvent): void {
			var perc:int = int((e.bytesLoaded/e.bytesTotal) * 100)					
			progress(perc, e.bytesLoaded*0.001, e.bytesTotal*0.001)
		}
		var onIOError_fn: Function = function (e: IOErrorEvent): void {
			loader.removeEventListener(Event.COMPLETE, loaded_fn)
			loader.removeEventListener(IOErrorEvent.IO_ERROR, onIOError_fn)
			loader.removeEventListener(ProgressEvent.PROGRESS, progress_fn)
			if (error != null) {
				error(e.text)
			}
			trace("Error downloading file.", e.text)
		}
		
		if(error != null){
			loader.addEventListener(IOErrorEvent.IO_ERROR, onIOError_fn)
		}
		if(progress != null){
			loader.addEventListener(ProgressEvent.PROGRESS, progress_fn)
		}
		loader.addEventListener(HTTPStatusEvent.HTTP_RESPONSE_STATUS, function(e:HTTPStatusEvent):void{
			trace('HTTP Status while downloading file:', e.status)
		})
		loader.addEventListener(Event.COMPLETE, loaded_fn)
		loader.load(req)
		
		return loader
	}

Please let me know what could have gone wrong.
Or let me know other simpler way to edit an already uploaded xml file.

Thanks,
Tushar

Hi Tushar!

Could you provide error message which you receive from server?
Regards,
Kate.

Can’t get any other details from server other than Error 400.

This is a drawback of Flash I think
But I checked it on the HTTP error code definitions online. Below is what they say.

400 Bad Request
The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repeat the request without modifications.

Btw, thanks for a quick reply Kate.
I badly need this thing to work.

You can get the server response by using Charles Proxy. Before you use it through, you need to add the following line in your app initialization code:

Backendless.SITE_URL = “http://api.backendless.com”;

That line must be executed before you call Backendless.initApp

Regards,
Mark

Hi Kate,

I have changed the code a little bit.
Since file to download is xml, I have changed the URL request content type to ‘application/xml’
But still server returns me content type as ‘application/json’
Please see below: log

downloading url: https://api.backendless.com/xxxxxxxxxxxxxxxxx/v1/files/config/AppData.xml
data type is text or xml
HTTP Status while downloading file: 400
request header:: Access-Control-Allow-Headers : origin, application-id, application-type, content-type, secret-key, request, user-token
request header:: Access-Control-Allow-Methods : POST, GET, OPTIONS, PUT, DELETE, PATCH
request header:: Access-Control-Allow-Origin : *
request header:: Content-Type : application/json
request header:: Date : Mon, 11 Jan 2016 14:59:39 GMT
request header:: Server : nginx/1.4.2
request header:: Content-Length : 62
request header:: Connection : keep-alive

Can you post a valid URL for the file you are trying to download?

Could you try to remove Content-Type header and send request without it?

Hi Mark,

Valid url is, https://api.backendless.com/2C268F11-55C9-113B-FF15-F291A39B4400/v1/files/config/AppData.xml

This url returns error {“code”:6007,“message”:“The specified resource was not found”}.
Could you check if this file exists in your file service storage (in app with provided app id)?

Hi Kate,

I have tried downloading even without Content-Type header, still server returns Content-Type as application/json.
Btw, in the log mentioned above in my post headers returned by server are response headers and not request headers. By mistake I have added a label as ‘request header::’

Mark, I am using REST API and not Actionscript SDK.

ActionScript SDK is very much incomplete as device registration for Push is not working, file downloading doesn’t work etc.
Let me know when AS3 SDK is fully functional.

Thanks
Tushar

I have tried 10 times downloading the same file with above code without giving any content-type.
Out of 10 time file downloaded successfully 3 times (response header of content-type returned by server was application/xml) and failed 7 times (response header of content-type returned by server was application/json).

Please look into this. It is very urgent.

Thanks,
Tushar

Finally I can reproduce this issue. We will try to fix it asap.

One more update.

I renamed the file to AppData.config and it downloaded only 1 time out of 6 times (content type was returned as application/json)
I renamed the file to AppData.png and it downloaded 6 time out of 7 times. (content type was returned as image/png)
Hope this helps

Could you try it again?

Yes. It is working fine now!

Thanks for such prompt support.

Tushar