PUT请求与子文档

huangapple go评论82阅读模式
英文:

PUT request with subdocuments

问题

我目前正在为我的后端API创建一个PUT请求,允许用户向数据集中添加餐厅。目前,在数据集中,餐厅的纬度和经度位于geometry > location下。我不确定如何在后端API中指定这一点。

以下是我目前的后端API代码:

  1. @app.route("/api/v1.0/restaurant", methods=["POST"])
  2. def add_new_restaurant():
  3. if "restaurant" in request.form and "city" in request.form:
  4. new_restaurant = {
  5. "restaurant": request.form["restaurant"],
  6. "city": request.form["city"],
  7. "reviews": []
  8. }
  9. new_restaurant_id = restaurants.insert_one(new_restaurant)
  10. new_restaurant_link = "http://localhost:5000/api/v1.0/restaurants/" + str(new_restaurant_id.inserted_id)
  11. return make_response(jsonify({"url": new_restaurant_link}), 201)

希望这对你有所帮助。

英文:

I'm currently creating a PUT request for my backend API to allow users to add restaurants to a dataset. Currently, in the dataset, the lat and lng of the restaurant is under geometry > location. Im unsure how I would specify this in the backend API.

PUT请求与子文档

Here is what I have for the backend API so far:

  1. @app.route("/api/v1.0/restaurant", methods = ["POST"])
  2. def add_new_restaurant():
  3. if "restaurant" in request.form and "city" in request.form:
  4. new_restaurant = {
  5. "restaurant": request.form["restaurant"],
  6. "city": request.form["city"],
  7. "reviews": []
  8. }
  9. new_restaurant_id = restaurants.insert_one(new_restaurant)
  10. new_restaurant_link = "http://localhost:5000/api/v1.0/restaurants/" \ + str(new_restaurant_id.inserted_id)
  11. return make_response( jsonify( { "url" : new_restaurant_link } ), 201 )

答案1

得分: 0

这是嵌套对象,因此您可以将它插入为如下所示。您可以用一个映射到一个 dict 值的 geometry 键表示它,该值具有映射到另一个指定经纬度的 dict 的 location 键。

  1. new_restaurant = {
  2. "restaurant": request.form["restaurant"],
  3. "city": request.form["city"],
  4. "reviews": [],
  5. "geometry": {
  6. "location": {
  7. "lat": LAT_VALUE,
  8. "long": LONG_VALUE
  9. }
  10. }
  11. }
英文:

It's a nested object so you can insert it as such. You can represent it in a dict with a geometry key that maps to a dict value, which has a location key that maps to another dict that specifies that lat/long.

  1. new_restaurant = {
  2. "restaurant": request.form["restaurant"],
  3. "city": request.form["city"],
  4. "reviews": [],
  5. "geometry": {
  6. "location": {
  7. "lat": LAT_VALUE,
  8. "long": LONG_VALUE
  9. }
  10. }
  11. }

huangapple
  • 本文由 发表于 2023年2月6日 19:58:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75361024.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定