Python Google App Engine Receiving a string in stead of JSON object

2024/10/8 18:32:32

I am sending a HTTP POST request from android to a server using the script below

            URI website = new URI("http://venkygcm.appspot.com");HttpClient client = new DefaultHttpClient();HttpPost request = new HttpPost(website);request.setHeader("Content-Type", "application/json");String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());JSONObject obj = new JSONObject();obj.put("reg_id","Registration ID sent to the server"); obj.put("datetime",currentDateTimeString);StringEntity se = new StringEntity(obj.toString());request.setEntity(se);HttpResponse response = client.execute(request);String out = EntityUtils.toString(response.getEntity()); 

As I have sent a JSON Object, I must receive a JSON Object in the server. Instead I get a string containing the data of the body. The server is made in Python Google App Engine.

 import webapp2class MainPage(webapp2.RequestHandler):def post(self):self.response.out.write(" This is a POST Request \n")req = self.requesta = req.get('body')self.response.out.write(type(a))app = webapp2.WSGIApplication([('/', MainPage)], debug=True)

I tried what AK09 suggested but i still get a string kind of object. What should be my next step?

import webapp2
import jsonclass MainPage(webapp2.RequestHandler):def post(self):self.response.out.write("This is a POST Request \n")req = self.requesta = req.get('body')b = json.dumps(a)self.response.out.write(type(a))self.response.out.write(type(b))app = webapp2.WSGIApplication([('/', MainPage)], debug=True)
Answer

Finally this code worked

import webapp2
import jsonclass MainPage(webapp2.RequestHandler):def post(self):self.response.out.write("This is a POST Request \n")req = self.requesta = req.bodyb = json.loads(a)self.response.out.write(b)self.response.out.write(b['reg_id'])self.response.out.write(b['datetime'])self.response.out.write(type(b))app = webapp2.WSGIApplication([('/', MainPage)], debug=True)

b comes out to be of the type List as is required.

https://en.xdnf.cn/q/118683.html

Related Q&A

Creating Gui for python client server

Help needed with my python project. Unable to get the code for the client or server implemented with the Gui i created. it is one based on book seller where the client is the buyer and the server is th…

Maximum Subarray sum - Where is my solution wrong? Kadanes Algorithm

Here is a description of the problem:The maximum sum subarray problem consists in finding the maximum sum of a contiguous subsequence in an array or list of integers: max_sequence([-2, 1, -3, 4, -1, 2,…

Pandas: Selecting rows and columns based on a subset of columns that contain a certain value

Lets say I have a dataframe with column names as follows:col_id_1, col_id_2, ..., col_id_m, property_1, property_2 ..., property_nAs an example, how would I search across all col_ids for, say, the valu…

Open a image file then display it in new window

I have a button in my GUI then after selecting a image file I want it to display in new window.Code:import tkinter as tk from tkinter import * from tkinter import ttk from tkinter import filedialog …

python plotting chart in interactive viewer vscode

This works and shows a plot in vscode: #%% cell with plot import matplotlib.pyplot as plt y = [3.2, 3.9, 3.7, 3.5, 3.02199] x = [0.15, 0.3, 0.45, 0.6, 0.75] n = [155, "outliner", 293, 230, 67…

Find all lines in a dataframe that matches specific pattern and extract the strings after split

I have a dataframe that looks like LineEntry: [0x0000000002758261-0x0000000002758268): /a/b/c:7921:14 LineEntry: [0x0000000002756945-0x0000000002756960): /f/b/c:6545:10 LineEntry: [0x00000000027562c9-0…

Python: Concatenate many dicts of numpy arrays with same keys and size

I have a function called within a loop that returns a dict (dsst_mean) with roughly 50 variables. All variables are numpy arrays of length 10.The loop iterates roughly 3000 times. Im current concatenat…

intersection of 2 objects of different types

i want to take the intersection of: ((e, 13.02338360095244), (a, 11.820318700775383), (o, 9.20172171683253), (s, 7.635081506807498), (n, 7.547469320471335), (i, 7.219915745772025), (r, 6.70492704072287…

Enemy Projectiles Attack Way To Fast Problem

I am trying to make my enemy bullets attack the player but its attacking way to fast I dont know why VIDEO my enemy bullets class# enemys bulletsksud = pygame.image.load("heart.png")class Boo…

How to find the maximum digit of an integer? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 10 years ago.This p…