Retrofit을 사용하는 Android 앱에서 MongoDB 문서를 업데이트해야하는 Flask API로 PUT 요청을 보내고 싶습니다. 전체 URL로 curl 명령을 실행하면 잘 작동하지만 개조 기능을 실행할 때 아무 일도 일어나지 않습니다.
서버의 로그를 보면 모든 GET 요청을 볼 수 있지만 PUT 요청은 볼 수 없습니다.
flask | [2020-06-18 19:07:16 +0000] [19] [DEBUG] GET /beers/all flask | [2020-06-18 19:07:30 +0000] [20] [DEBUG] GET /beers/by_barcode flask | [2020-06-18 19:07:30 +0000] [10] [DEBUG] GET /beers/by_id
이 curl 명령을 사용하면
curl -X PUT 'http://0.0.0.0:5000/users/add_scanned?user_id=test2&beer_id=test2C&beer_name=test2&img_link=http://test2'
모든 것이 예상대로 작동하며 다음 로그가 표시됩니다.
flask | [2020-06-18 19:26:49 +0000] [11] [DEBUG] PUT /users/add_scanned flask | [2020-06-18 19:26:49 +0000] [11] [INFO] Adding beer scanned
Mongo는 이상한 것을 기록하지 않습니다. Retrofit의 또 다른 PUT 요청은 매력처럼 작동합니다.
flask | [2020-06-18 19:10:38 +0000] [20] [DEBUG] PUT /beers/update_barcode
다음은 내 코드입니다.
Android Retrofit API
import com.squareup.moshi.Moshi
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import retrofit2.Call
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory
import retrofit2.converter.scalars.ScalarsConverterFactory
import retrofit2.http.GET
import retrofit2.http.POST
import retrofit2.http.PUT
import retrofit2.http.Query
private const val BASE_URL = "http://***.***.**.*:5000/"
private val moshi = Moshi.Builder()
.add(KotlinJsonAdapterFactory())
.build()
private val retrofit = Retrofit.Builder()
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(MoshiConverterFactory.create(moshi))
.baseUrl(BASE_URL)
.build()
interface UserApiService {
/**
* Adds a beer to scanned list
*/
@PUT("users/add_scanned")
fun addScanned(
@Query("user_id") userId: String,
@Query("beer_id") beerId: String,
@Query("beer_name") beerName: String
):
Call<String>
}
Flask 청사진
from flask import Blueprint, jsonify, request, abort
from bson.objectid import ObjectId
from pymongo import MongoClient
from flask import current_app
bp = Blueprint('users', __name__, url_prefix='/users')
client = MongoClient(host="mongo", username="*****", password="*****")
db = client.TeddyMongo
collection = db.users
def to_dict(document):
document["_id"] = str(document["_id"])
return document
# When a user is not found
@bp.errorhandler(404)
def user_not_found(e):
return jsonify(error=str(e)), 404
# Add beer scanned
@bp.route('/add_scanned', methods=['PUT'])
def add_scanned():
user_id = request.args.get('user_id')
scanned = {
"_id" : request.args.get('beer_id'),
"name" : request.args.get('beer_name')
}
if request.args.get('img_link'):
scanned["img_link"] = request.args.get('img_link')
current_app.logger.info("Adding beer scanned")
collection.update_one({"_id":user_id}, { "$addToSet" : {"scanned" : scanned}}, upsert=True)
return "Beer added to scanned"