Home Ask Login Register

Developers Planet

Your answer is one click away!

NateDawg87 February 2016

HttpClient not POST-ing data to backend server- javascript

I am trying to POST data to a web server (Google app engine) from a windows phone using javascript. The request is reaching the server, but the values are all going as null.

The POST request:

var payload = '{"type":"a","make":"b","model":"c","year":"1990"}';
var uri = new Windows.Foundation.Uri(uriString);
var stringContent = Windows.Web.Http.HttpStringContent(payload, Windows.Storage.Streams.UnicodeEncoding.utf8, 'application/json');
httpClient.postAsync(new Windows.Foundation.Uri(uri), stringContent);

If I make the properties required=False they are null. If I make required=True I get BadValueError: Entity has uninitialized properties: vehicle_type, make, model, year

class Vehicle(ndb.Model):
vehicle_type = ndb.StringProperty(required=False)
make = ndb.StringProperty(required=False)
model = ndb.StringProperty(required=False)
year = ndb.StringProperty(required=False)

The goal is to have the new vehicle added to the datastore as such:

{"keys": 6212341628665856, "make": "Dodge", "vehicle_type": "SUV", "model": "Caravan", "year": "1999"}

If I use a curl call to the server it works just fine:

curl -X POST --data-urlencode "type=SUV" --data-urlencode "make=Dodge" --data-urlencode "model=Caravan" --data-urlencode "year=1999" myUrl

All help is appreciated


Ankur Choraywal February 2016

Try to use logging module and see whether all the values you receive in backend are correct or not.

first of all import logging module.

import logging

second thing, you need to use logging module to see whether the values you get in backend are upto the mark. suppose you have a webapp2.RequestHandler as

import webapp2

class GetCarInfo(webapp2.RequestHandler):
 def get(self): #or post 
  #show all values in gae sdk console
  #or logging.info(self.request.get('<your payload object key>'))

  #your entity creation(ndb put) code here.

app = webapp2.WSGIApplication([
    ('/add_car_info', GetCarInfo)
], debug=True)

Once you are able to see correct info that is being passed from javascript client. You can proceed further else you need to figure out what's wrong with your javascript client(ajax request) or payload you are passing.

Hope this help you.!

Post Status

Asked in February 2016
Viewed 1,583 times
Voted 9
Answered 1 times


Leave an answer

Quote of the day: live life