Ahoy is not storing events in the DB but there is a POST request in the logs - ahoy

I am having issues with getting Ahoy to register information to the database, but the logs are showing a post request:
Started POST "/ahoy/events" for 54.163.248.147 at 2015-08-05 22:43:41 +0000 Processing by Ahoy::EventsController#create as JSON Parameters: {"_json"=>[{"id"=>"34e8f9b6-a85f-4e15-a7f4-0992fd68cb71", "name"=>"$view", "properties"=>{"url"=>"http://unshakable-missile-106309.nitrousapp.com:3000/", "title"=>"Trendosaur", "page"=> "/"}, "time"=>1438814619.857}], "event"=>{}}
Any ideas why this isn't saving to the database?
Thanks!

Alright worked it out, for some reason in my initialiser it had:
class Ahoy::Store < Ahoy::Stores::LogStore
As opposed to:
class Ahoy::Store < Ahoy::Stores::ActiveRecordStore

Related

django request.POST.items() returns empty list everytime with Postman

I have a django method and I'm just trying to pull out a POST variable:
#csrf_exempt
def update_backer(request):
for k, v in request.POST.items():
print(k, v)
email = request.POST.get("email", "none")
return JsonResponse({"data":{
"email":email
}})
When I try to do a POST via javascript XMLHttpRequest, I wasn't getting the data through. So I fell back to Postman to try to confirm things are working on the django end. And I am posting the data, but django isn't seeing it. Am I doing something obviously wrong?
Edit: Interestingly enough, if I change it to GET instead of POST, it works as I would expect.
For POST request values are sent in request body. You can use application/x-www-form-urlencoded content type so that request body has same format of query params.

how to get access to post data incoming inside a Django middleware?

I am writing a middleware due to which i need to access post data but none of the answers are solving my doubt, i am reading the data like this but this is working in postman but giving error in browser
dict_str = request.body.decode("UTF-8")
any help is highly appreciated
error
RawPostDataException at /accounts/api/v1/register/
You cannot access body after reading from request's data stream
You can get by reqeust.data, once POST body parsed on DRF, accessing to body is hindered.
You can try this, i hope this will work
import json
import ast
def CustomMiddleware(get_response):
def middleware(request):
bytes_request_data = getattr(request, '_body', request.body)
if bytes_request_data:
request_data = ast.literal_eval(bytes_request_data.decode('utf-8'))
print(request_data)
This exception is raised if you try to access the body after reading the request with HttpRequest.read() or HttpRequest.readline().
The request is probably read in a previous middleware.
From Django's documentation https://docs.djangoproject.com/en/4.1/ref/request-response/#django.http.HttpRequest.POST:~:text=You%20can%20also%20read%20from%20an%20HttpRequest%20using%20a%20file%2Dlike%20interface%20with%20HttpRequest.read()%20or%20HttpRequest.readline().%20Accessing%20the%20body%20attribute%20after%20reading%20the%20request%20with%20either%20of%20these%20I/O%20stream%20methods%20will%20produce%20a%20RawPostDataException.:
You can also read from an HttpRequest using a file-like interface with HttpRequest.read() or HttpRequest.readline(). Accessing the body attribute after reading the request with either of these I/O stream methods will produce a RawPostDataException.

Django Rest Framework Not Returning Full JSON

I am having trouble retrieving the full JSON Response when executing a GET request on an API I am building with DRF. If I include pagination and retrieve, say, 100 results then I receive the full JSON Response. If I do not use pagination, and would like to retrieve a few thousand results, then the server simply cuts off at a seemingly random spot and does not return full JSON. For instance, it may return {"hi": "hel instead of returning {"hi":"hello"}. DRF reports this as a 200 response code, so it seems as though it executes properly.
The code for my view looks like:
class RepresentativeListView(generics.ListAPIView):
queryset = models.Representative.objects.all()
serializer_class = serializers.RepresentativeSerializer
The code for my serializer looks like:
class RepresentativeSerializer(serializers.ModelSerializer):
class Meta:
model = models.Representative
fields = (
'bioguide_id',
'stats',
'leadership_score',
'ideology_score',
)
Could anyone understand why a full JSON response would not be coming through?
This might be an issue with your Apache / Nginx / whatever you use web server.
It may close the connection if the application didn't respond on time.
Check your web server logs and the time it takes for the application to render the response vs the server timeout.

How To fire POST Request with params and token

I am new to API testing with jayway RestAssured.
my jmeter url : http://ip:8080/servelet?token=toekntext&methodname={jsontext}
above url is POST Request, i need to fire request in jayway RestAsseured.
url = http://ip:8080/servelet
Response r = given().contentType(CONTENT_TYPE).accept(CONTENT_ACCEPT).headers("user-agent", web).queryParam("token", tokentext).queryParam("methodname", jsonttext).expect().statusCode(200).when().post(url);
Is the above code correct to fire POST Request Here i am getting 500 internal server error, plz help me.
Yes that looks right given that it truly are query parameters that JMeter is sending. I suspect that it might not be since it's very unusual in my experience that include JSON (I assume jsontext is JSON) in the request path. Try switching from queryParam to formParam to see if it makes any difference.
Try restructuring your code,
FULL-URL - url/account?token=TOKEN&sync=TRUE, then you can try post request as below
given().
contentType(ContentType.JSON).body(payload).
queryParam("token", "TOKEN").
queryParam("sync", "TRUE").
when().post(url).then().
statusCode(200).extract().response();

How to send data as key - value pairs instead of string via POST using XHR

I'm creating two POST calls. One using a django form and one using angular js via a resource xhr.
The angular setup looks like this:
myModule.factory('gridData', function($resource) {
//define resource class
var root = {{ root.pk }};
var csrf = '{{ csrf_token }}';
return $resource('{% url getJSON4SlickGrid root.pk %}:wpID/', {wpID:'#id'},{
get: {method:'GET', params:{}, isArray:true},
update:{method:'POST', headers: {'X-CSRFToken' : csrf }}
});
});
With creating an xhr post request as such:
item.$update();
This post request is send to the server as expected, but when I want to access the QueryDict I cannot access the data passed using:
name = request.POST.get('name', None)
name is always None like this.
The issue behind this is that the QueryDict object is getting parsed quite strange.
print request.POST
<QueryDict: {u'{"name":"name update","schedule":0"}':[u'']}>
Whereas I would have expected this result, which I got when I send the data via a "normal" Post request:
<QueryDict: {u'name': [u'name update'], u'schedule': [u'0']}>
So it seems to be that Django receives something in the POST request which instructs Django to parse the parameters into one string. Any idea how to circumvent this?
Update:
I found this discussion where they say that the issue is if you provide any content type other than MULTIPART_CONTENT the parameters will be parsed into one string. I checked the content-type send with the POST request and it is really set to 'CONTENT_TYPE': 'application/json;charset=UTF-8'. Thus this is likely the issue. Therefore my question is: How can I set the CONTENT_TYPE for a xhr post request created using angular.js resources to MULTIPART_CONTENT?
you could either:
fiddle with the client to send data instead of json
use json.loads(request.raw_post_data).get('name', None) (django < 1.4)
use json.loads(request.body).get('name', None) (django >= 1.4)
The Angular documentation talks about transforming requests and responses
To override these transformation locally, specify transform functions as transformRequest and/or transformResponse properties of the config object. To globally override the default transforms, override the $httpProvider.defaults.transformRequest and $httpProvider.defaults.transformResponse properties of the $httpProvider.
you can find an example here as was previously pointed at.