帖子方法使用请求返回422,但与Curl和Postman一起工​​作正常

发布于 2025-02-08 06:06:47 字数 1747 浏览 0 评论 0原文

我正在使用Python和请求模块连接到EasyRedmine API。我很难使用邮政方法来创建一个项目以发布JSON。它返回“ [无法加工的实体 <响应[422]>] “。我已经使用Postman和Curl尝试了相同的请求,并且它可以很好地工作。有人知道可能导致这个问题吗?以下curl命令正常工作。

curl -x'post'
'https://cafte.easyredmine.com/projects.json?key=0224b4238xxxx'
-h'accept:application/json'
-h'X-REDMINE-API-KEY:0224B4238XXX'
-h'content-type:application/json'
-d' { “项目”: { “ easy_external_id”:“外部系统1”, “名称”:“蓝色2”, “主页”:“蓝色7”, “描述”:“我不能遵守这些爪哇。令人恶心的生物。 “ fure_id”:0, “ is_planned”:是的, “ easy_is_easy_template”:是的, “ easy_currency_code”:“ eur” }

这是methode代码:

def create_project(self, name: str, description="", external_id="", homepage="", parent_id="", author_id="",
                   is_planned= 'false', is_easy_template= "true", start_date="", due_date="", custom_fields=[],
                   tag_list=[], currency='EUR'):
    print(name)

    data = {"project": {
        "easy_external_id": external_id,
        "name":name,
        "homepage": homepage,
        "description": description,
        "parent_id": parent_id,
        "author_id": author_id,
        "is_planned": is_planned,
        "easy_is_easy_template": is_easy_template,
        "easy_start_date": start_date,
        "easy_due_date": due_date,
        "custom_fields": custom_fields,
        "tag_list": tag_list,
        "easy_currency_code": currency
    }
    }


    endpoint_path = "/projects"
    endpoint = f'{self.base_url}{endpoint_path}'
    headers = {'X-Redmine-API-Key': f'{self.api_key}',
               'accept': 'application/json',
               'Content-Type': 'application/json'
               }


    r = requests.post(endpoint, headers=headers, json= data)

I am using Python with Requests module to connect to an EasyRedmine API. I'm having trouble creating a project using the post method in order to post a JSON. It returns "[Unprocessable Entity
<Response [422]>]
". I have tried the same request using Postman and Curl and it works perfectly. Does anyone know what could be causing this problem? The following curl command works fine.

curl -X 'POST'
'https://cafte.easyredmine.com/projects.json?key=0224b4238XXXX'
-H 'accept: application/json'
-H 'X-Redmine-API-Key: 0224b4238XXX'
-H 'Content-Type: application/json'
-d '{
"project": {
"easy_external_id": "external-system-1",
"name": "Blue 2",
"homepage": "blue-7",
"description": "I can’t abide these Jawas. Disgusting creatures.",
"author_id": 0,
"is_planned": true,
"easy_is_easy_template": true,
"easy_currency_code": "EUR"
}

This is the methode code:

def create_project(self, name: str, description="", external_id="", homepage="", parent_id="", author_id="",
                   is_planned= 'false', is_easy_template= "true", start_date="", due_date="", custom_fields=[],
                   tag_list=[], currency='EUR'):
    print(name)

    data = {"project": {
        "easy_external_id": external_id,
        "name":name,
        "homepage": homepage,
        "description": description,
        "parent_id": parent_id,
        "author_id": author_id,
        "is_planned": is_planned,
        "easy_is_easy_template": is_easy_template,
        "easy_start_date": start_date,
        "easy_due_date": due_date,
        "custom_fields": custom_fields,
        "tag_list": tag_list,
        "easy_currency_code": currency
    }
    }


    endpoint_path = "/projects"
    endpoint = f'{self.base_url}{endpoint_path}'
    headers = {'X-Redmine-API-Key': f'{self.api_key}',
               'accept': 'application/json',
               'Content-Type': 'application/json'
               }


    r = requests.post(endpoint, headers=headers, json= data)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文