谷歌云功能的HTTP触发器存在问题

2024-04-16 21:02:00 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的密码

import json
from flask import escape
def location_sort(request):
    request_json = request.get_json()
    location = json.dumps(request_json)
    location = json.loads(location)
    reverse_location = {v: k for k, v in location.items()}


    x = location.keys()
    harf_x = (float(max(x)) + float(min(x))) / 2 

    y_right = []
    y_left = []
    sorted_location = [] 


    for i in location:
        if float(i) < harf_x:
            y_left.append(location[i])
        else: 
            y_right.append(location[i])

    y_left.sort() 
    y_right.sort(reverse=True) 

    sorted_input = y_left + y_right 


    for i in sorted_input:
        sorted_location.append([reverse_location[i], i])
    for i in sorted_location:
        i[0],i[1] = float(i[0]),float(i[1])
    return sorted_location

def cal_centroid(location):
    area = 0 # 면적
    centroid_x = 0 
    centroid_y = 0 
    temp = 0

    for i in range(len(location)):
        if i == len(location)-1: 
            temp = location[i][0]*location[0][1] - location[0][0]*location[i][1]
            area += temp*0.5
            centroid_x += (location[i][0] + location[0][0]) * temp
            centroid_y += (location[i][1] + location[0][1]) * temp
        else:
            temp = location[i][0]*location[i+1][1] - location[i+1][0]*location[i][1]
            area += temp*0.5
            centroid_x += (location[i][0] + location[i+1][0]) * temp
            centroid_y += (location[i][1] + location[i+1][1]) * temp

    centroid_x = round(centroid_x / (6*area), 6)
    centroid_y = round(centroid_y / (6*area), 6)
    x = [centroid_x, centroid_y]
    return json.dumps(x)

def main(request):
    a = location_sort(request)
    return cal_centroid(a)

我用main运行我的代码。通过在google云函数上测试代码,我可以使用触发器事件框输入参数。但我想在使用url时输入参数。如下图所示

https://YOUR_REGION-YOUR_PROJECT_ID.cloudfunctions.net/FUNCTION_NAME?request={"37.284213":"127.006481","37.562045":"127.034809","37.528694":"126.907483","37.411124":"127.124356"}

我应该如何更改代码以使其表现为那样?我很难理解关于谷歌功能的谷歌文档。谢谢你的帮助


Tags: inrightjsonforrequestdefarealocation
1条回答
网友
1楼 · 发布于 2024-04-16 21:02:00

您需要在请求正文中使用JSON而不是URL参数发出POST请求。例如,使用curl

curl -X POST https://YOUR_REGION-YOUR_PROJECT_ID.cloudfunctions.net/FUNCTION_NAME -H "Content-Type:application/json"  -d '{"37.284213":"127.006481","37.562045":"127.034809","37.528694":"126.907483","37.411124":"127.124356"}'

相关问题 更多 >