Can one cancel a multipartFormData upload Alamofire request? - multipartform-data

I have a multipartFormData upload request that I want to store in a property so that I can allow the user to cancel it at any moment.
It looks like this :
var uploadRequest : Alamofire.Request?
self.uploadRequest = Alamofire.upload(.POST, apiURL, multipartFormData: { multipartFormData in
I'm receiving the following error though :
type of expression is ambiguous without more context
For the Alamofire HTTP method param.
What am I missing?

Try specifying Method.POST or Alamofire.Method.POST instead of just .POST

Related

Axios .get request weird behaviour

I have the following get request:
return axios
.get<ArticlesResponse>(SUGGESTED_ARTICLES, {
headers: {
'Content-Type': 'application/json',
},
})
.then(onRequestSuccess)
.catch(onRequestError);
It returns me an object with the data I need, however, the data field inside the object is a string instead of an actual object. Anyone has any ideea about why? I looked it up and saw that adding that header above will fix the issue but it doesn't. Thanks in advance!
My onRequestSucces is:
export function onRequestSuccess<T = any>(response: AxiosResponse<T>) {
console.log('Request Successful!', response);
return response.data;
}
JSON.Parse() also won't fix it.
The problem may be due to the API returning a response that contains invalid JSON data, now JSON.parse would throw an error, but Axios manages the exception by setting the invalid JSON as string in the data property. Try using the Fetch API.
Since you're using a GET request (doesn't have a body) the 'Content-Type' is not being useful. This header is used to tell the server which type of content you're sending, but you're sending none. You should use it only on POST/PUT requests.
See this question for more details on this.
In order for your request to be read as JSON you have to set the header in the server. This will tell the browser you're receiving a JSON, which will then be parsed automatically by axios.

How to use / in parameter during rest webservice call from angularjs?

I need to send a value like app/role as parameter through rest webservice url from angularjs
In controller.js
var roleName = 'app/role';
checkRole.check({'roleName': roleName}, function(data){}
In model.js
popModel.factory('checkRole', function ($resource) {
return $resource('./rest/checkRole/:roleName',{roleName:'#roleName'},{
check: {'method':'GET'},
});
});
The rest webservice call in java
#GET
#Path("/checkRole/{roleName}")
#Produces(MediaType.APPLICATION_JSON)
public Response checkRole(#Context HttpServletRequest request, #PathParam("roleName") String roleName);
When i pass it i am getting browser console error as
Bad request response from the server.
For normal parameter values like 'Test', 'Solution', 'Application' etc. If i use with / as a parameter no process is done and i am getting error.
/ is reserved character for GET request. So, you can't use them directly. If you use them, you would get Bad Request Error.
One of the solution can be to encode the URL on client side and decode it on server.
Reference:
Characters allowed in GET parameter
How to handle special characters in url as parameter values?

API key Google API url get request doesn't work

I'm trying to access data from Google API but it's returning a 403 okay error. Also, when I put the API key in between braces, i get a 404 error. My API key is a browser type and I pass the value in after "key=". I am sure my url is correct as I tested it in the tutorial and it returns a 202, which makes me think that something is wrong with the API key. Here's the code I'm using in my AngularJS HTTP get request:
var election= "https://www.googleapis.com/civicinfo/v2/elections?key=" + API_KEY;
$http.get(election)
.success(function(data) {
console.log(data);
});
Any ideas on why this is happening?
It looks like it is a syntax error, add some single quote parenthesis around the url like this:
var election= 'https://www.googleapis.com/civicinfo/v2/elections?key="' + API_KEY;

Restangular call

Restangular.all('patientService/edit').post($scope.patientService).then(function(patientService){
$modalInstance.close(patientService);
$route.reload()
});
where patientService/edit is a full path to the called method.(patientService) for class and (edit) for edit method .
It gives me an error 400 Bad Request
cannot we send this kind of request in restangular
You might wish to look at the variables being passed in patientService. If the server is trying to map them to a class you will get a 400 if they are misspelt, do not exist or have incompatible data types.
In Restangular I would separate "/" :
Restangular.all('patientService').all('edit').post($scope.patientService).then(function()
{
});
But your problem could be elsewhere : perhaps this REST path couln't be understood server-side ?

How to know if a Kohana request is an internal one?

I'm writing an API using Kohana. Each external request must be signed by the client to be accepted.
However, I also sometime need to do internal requests by building a Request object and calling execute(). In these cases, the signature is unnecessary since I know the request is safe. So I need to know that the request was internal so that I can skip the signature check.
So is there any way to find out if the request was manually created using a Request object?
Can you use the is_initial() method of the request object? Using this method, you can determine if a request is a sub request.
Kohana 3.2 API, Request - is_initial()
It sounds like you could easily solve this issue by setting some sort of static variable your app can check. If it's not FALSE, then you know it's internal.
This is how I ended up doing it: I've overridden the Request object and added a is_server_side property to it. Now, when I create the request, I just set this to true so that I know it's been created server-side:
$request = Request::factory($url);
$request->is_server_side(true);
$response = $request->execute();
Then later in the controller receiving the request:
if ($this->request->is_server_side()) {
// Skip signature check
} else {
// Do signature check
}
And here is the overridden request class in application/classes/request.php:
<?php defined('SYSPATH') or die('No direct script access.');
class Request extends Kohana_Request {
protected $is_server_side_ = false;
public function is_server_side($v = null) {
if ($v === null) return $this->is_server_side_;
$this->is_server_side_ = $v;
}
}
Looking through Request it looks like your new request would be considered an internal request but does not have any special flags it sets to tell you this. Look at 782 to 832 in Kohana_Request...nothing to help you.
With that, I'd suggest extending the Kohana_Request_Internal to add a flag that shows it as internal and pulling that in your app when you need to check if it is internal/all others.
Maybe you are looking for is_external method:
http://kohanaframework.org/3.2/guide/api/Request#is_external
Kohana 3.3 in the controller :
$this->request->is_initial()
http://kohanaframework.org/3.3/guide-api/Request#is_initial

Resources