I am using ajax to pass an array data to Django (1.10 under Python 3.5) view for further processing. However, I noticed that the passing is not success and results in None. I am wondering what place I have gone wrong and can anyone suggest solution to this. Thanks a lot. Details are as below:
Ajax query to pass array final_ele to view as the variable final_ele_view:
$.ajax({
url: "../exportNews/",
method: "POST",
data:
{
final_ele_view: 'final_ele',
csrfmiddlewaretoken: '{{ csrf_token }}'
}
});
Then in the urls.py:
urlpatterns = [
url(r'^admin/exportNews/', views.exportNews),
...
Then in views.exportNews:
from annoying.decorators import ajax_request
#ajax_request
#csrf_protect
def exportNews(request):
arr_tag = request.POST.getlist('final_ele_view[]')
return HttpResponse(arr_tag)
By the way, I just notice one thing. Even in the ajax I ask for the POST method, but in views.py which shows the method is actually GET (by using return HttpResponse(request.method) ). Is that the reason of the error, and why this happens?
Why the [] at the end of fiinal_ele_view[]? I bet the QueryDict returned from request.POST does not know how to handle square brackets because that's invalid Python syntax.
Related
I want to create a character, with a name and an avatar. Here is my code :
CharacterApi.js, in which I make my network call
function addCharacter(name, avatar) {
const data = new FormData();
data.append(‘name’, name);
data.append(‘avatar’, avatar);
return authenticatedFetch({
path: '/teams/characters’,
method: 'post',
data
});
}
characters_controller.rb
def create
#character = #character.new(character_params)
if #character.save
render :show
else
render json: #character.errors, status: :unprocessable_entity
end
end
[. . .]
private
def character_params
params.fetch(:character, {}).permit(:name, :avatar)
end
Which, when I make my create request, gives me a 422: unprocessable entity error. However, debugs showed me that the right parameters are actually sent, and doing the following makes everything working fine again :
def create
#character = #characters.new
#character.name = params[:name]
#character.avatar = params[:avatar]
[. . .]
end
Even though this second method works, it is not very practical to me, since I’d like to add a few more params in the future.
If I send the params as simple JSON, I can’t get any file (always gives me a null value), but the first method works, this time :
With this I can’t send images or any files, but the initial rails syntax works again
function addCharacter(name, avatar) {
return authenticatedFetch({
path: '/teams/characters’,
method: 'post',
data: {name, avatar}
});
}
Is there a reason why the short syntax #character.new(character_params) isn’t working when I send FormData?
I use carrier wave to handle files in rails API. Thanks in advance for any help!
You are using Strong Parameters the wrong way.
It must be:
def character_params
params.require(:character).permit(:name, :avatar)
end
I'm using angular 1.5.7 and perl 5.16.2. I don't have the ability to use any external libraries or switch tools - I'm stuck just using what's included with these two.
The angular controller is making calls to a perl file. I'm able to make POST requests successfully, and my GET requests are returning status 200 but aren't showing the data that I am expecting to see. Right now I'm just working with a simple example to try and narrow down where things are going wrong, and I think I just don't know the format to return values from the perl file.
My controller GET method looks like so:
$http({
method : "GET",
url : "filename.pl",
params:{}
}).then(function successCallback(response) {
console.log(response.records);
}, function errorCallback(response) {
console.log(response);
});
and the perl code being called in filename.pl is:
my $string = q{{"records":{"2":{"City":"México D.F.","Country":"Mexico","Name":"Ana Trujillo Emparedados y helados"},"1":{"Name":"Alfreds Futterkiste","Country":"Germany","City":"Berlin"}}}};
return $string;
The string is just a dummy variable to see if passing things back works - it won't be the final data. I have verified it is valid JSON as well. Here's what the response from the server looks like:
{"data":"","status":200,"config":{"method":"GET","transformRequest":[null],"transformResponse":[null],"url":"glwizardutils.esp","params":{"FUNCTION":"initcheckboxes","CONTEXTID":"432"},"headers":{"Accept":"application/json, text/plain, */*"}},"statusText":"OK"}
The data field is totally blank, and the response field doesn't work at all. I can verify it's hitting the perl file - like I said, POST requests work fine and I included some printouts that verify it's in the correct method, but it's just not returning anything. What should I be doing here instead to get this to work?
If filename.pl is invoked with HTTP, then it should be writing an HTTP response to standard output. At a minimum:
print "HTTP/1.1 200 OK\n";
print "Content-type: application/json\n";
print "\n";
my $string = q{{"records":{"2":{"City":"México ..."}}}};
print $string;
Several frameworks and modules exist in Perl and virtually every other language to handle the repetitive aspects of writing a proper response, which you will want to look into as your Perl server-side script gets more demanding.
I have a REST API endpoint for creating an empty object. What is the "standard" url scheme for this GET method? I'm currently using a factory in an angularjs app to make the call to the server.
Right now I have the following scheme:
GET
Item/new/
My $resource:
ngServices.factory("TESTfactory", function ($resource) {
return $resource("testNewItem/new", {}, {
create: {method: 'GET'}
}
}
A successful call to the above resource:
$scope.newItem = TESTfactory.newItem.create();
Any other suggestions would be much appreciated.
I've looked at the following links, which did not specifically list a url scheme for getting empty objects:
REST API Overview
Quick Reference section in this doc
A GET method should never create something. GET is supposed to be nullipotent, which means that it should have no side-effects. Creating a resource is certainly a side effect.
So, the standard http call would be
POST
Items/create
or
POST
Items/new
or better yet just
POST
Items/
I'm working on an API with FosRestBundle and AngularJS for the front part.
GET requests working but i have problems with POST requests. In the API controller the request object is empty..
Here is the front part
$http.post(Route.api + '/leads', {lead: "test"}).success(function(data) {
});
And here is the FosRestBundle controller
public function postAction(Request $request) {
var_dump($request->request->all()); // empty ? :(
}
Someone already have the same problem ?
Thanks ;)
Yes I had such problem, and I've solve this problem passing Content-Type header to Symfony, here is my code example
return $http({
method: 'POST',
url: Route.api + '/leads',
data: dataYouWantToSend, //see the note regarding this parameter
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
})
NOTE :
If you pass data parameter with object, you will receive in in the POST your data by JSON with key of your JSON's root.
If you want to receive it like form data, you have to convert it to querystring (e.g. ?deal=test&ok=1), for example if you're using jQuery too, you can convert it like this
var dataYouWantToSendQueryString = $.param(dataYouWantToSend);
hope it will help you too.
P.S. I've researched and haven't found how to convert object to query string in angular, so if Angular has such functionality, I will glad to know about it, if someone know, please comment.
I have a Cloud Endpoints method that looks like this:
//HTTP POST
#ApiMethod(name = "hylyts.insert")
public Hylyt insertHylyt(#Named("url") String url, Hylyt hylyt, User user)
throws OAuthRequestException{
log.info("Trying to save hylyt '"+hylyt+"' with id '"+hylyt.getId());
if (user== null) throw new OAuthRequestException("Your token is no good here.");
hylyt.setArticle(getArticleKey(url, user));
ofy().save().entity(hylyt);
return hylyt;
}
I call it from the Javascript Client Library using this:
gapi.client.hylytit.hylyts.insert({PARAMS}).execute(callback);
Now, if I structure {PARAMS} as suggested in the docs (second example),
{
'url': url,
'resource': {
'hylyt': {
'contentType': 'application/json',
'data': hylyt
}
}
}
I get a null object in the endpoint (not to mention that the whole point of this library is to make these calls simple, which this structure clearly violates).
When I structure {PARAMS} as these answers suggest,
{
'url': url,
'resource': hylyt
}
I get a null object in the endpoint again. The correct syntax is this:
{
'url': url,
'id': hylyt.id
'text': hylyt.text
}
Which just blows my mind. Am I doing this all wrong? Is this a bug? Is it only happening because gapi is also passing the auth token in the background?
Yes, I could use the request syntax instead, but, again, why even use the library if it's just as complex as making the XHRs in pure javascript? I wouldn't mind the complexity if Google explained in the docs why things are happening. But the docs, paraphrased, just say use these methods and the auth, CORS, and XHR magic will happen behind closed doors.
Is the API method correctly recognized as POST method?
The resource parameter which is sent as POST body won't work correctly in a GET request.
The way it looks you are actually sending a GET request with the Hylyt properties in the query string.
To make sure you can change the method annotation to this:
#ApiMethod(name = "hylyts.insert", httpMethod = HttpMethod.POST)
Yup, agreed it's a bug. caused me great pains as well.
So i guess the work around is to create a combined object to pass to your api all named and un named parameters. Rather than hardcode each.. a quick loop might be better.
var param = {};
param["url"] = url;
for (var prop in hylyt) {
param[prop] = hylyt[prop];
}
gapi.client.hylytit.hylyts.insert(param).execute(callback);
That mashing together of parameters / objects can become a slick function if you really want.. but it's a band aid for what I'd consider a defect.
I see in the related question (cloud endpoints resource attribute for transmitting named params & body not working), you actually logged a defect.. Good stuff. Though there still appears no movement on this one. fingers crossed for someday!
The bug has been resolved. The correct syntax is
gapi.client.hylytit.hylyts.insert({url: url}, hylyt).execute(callback);