Create webhook with gitea api and get RequiredError #5384

Closed
opened 2025-11-02 06:23:09 -06:00 by GiteaMirror · 4 comments
Owner

Originally created by @Sylor-huang on GitHub (May 12, 2020).

My environments are Rails and Mysql and Gitea

Gitea create webhook api :

# api 

/repos/{owner}/{repo}/hooks

# params: 

{
  "active": false,
  "branch_filter": "string",
  "config": {
    "additionalProp1": "string",
    "additionalProp2": "string",
    "additionalProp3": "string"
  },
  "events": [
    "string"
  ],
  "type": "dingtalk"
}

When I use this api to create webhook with custom params

I get errors which say RequiredError

# custom params: 

hook_params = {
      active: true,
      branch_filter: "*",
      config: {
        content_type: "application/json",
        url: "xx/repositories/2222/repo_hooks.json",
        http_method: "post"
      },
      events: ["create", "pull", "push"],
      type: "gitea",
    }

Response Errors Logs

...

I, [2020-05-12T10:19:32.254166 #86437]  INFO -- request: POST xxxx/api/v1/repos/ownner/repo_name/hooks
D, [2020-05-12T10:19:32.254238 #86437] DEBUG -- request: Content-Type: "application/json"
...
User-Agent: "Faraday v0.15.4"
I, [2020-05-12T10:19:32.698568 #86437]  INFO -- response: Status 422
D, [2020-05-12T10:19:32.698731 #86437] DEBUG -- response: server: "nginx/1.10.1"
date: "Tue, 12 May 2020 02:19:32 GMT"
content-type: "application/json; charset=utf-8"
content-length: "159"
connection: "close"
...
###############____response__#<Faraday::Response:0x00007f942fcd7330>
###############____response_status_422
###############____response_body_[{"fieldNames":["Type"],"classification":"RequiredError","message":"Required"},{"fieldNames":["Config"],"classification":"RequiredError","message":"Required"}]
...

I had changed hook_params.type to hook_params.Type, and the same to hook_params.Config, but it seems do not work also.

Is my hook_params wrong? and how to fix hook_params?
TKS~

Originally created by @Sylor-huang on GitHub (May 12, 2020). My environments are `Rails` and `Mysql` and `Gitea` ### Gitea create webhook api : ``` # api /repos/{owner}/{repo}/hooks # params: { "active": false, "branch_filter": "string", "config": { "additionalProp1": "string", "additionalProp2": "string", "additionalProp3": "string" }, "events": [ "string" ], "type": "dingtalk" } ``` ### When I use this api to create webhook with custom params I get errors which say `RequiredError` ``` # custom params: hook_params = { active: true, branch_filter: "*", config: { content_type: "application/json", url: "xx/repositories/2222/repo_hooks.json", http_method: "post" }, events: ["create", "pull", "push"], type: "gitea", } ``` ### Response Errors Logs ``` ... I, [2020-05-12T10:19:32.254166 #86437] INFO -- request: POST xxxx/api/v1/repos/ownner/repo_name/hooks D, [2020-05-12T10:19:32.254238 #86437] DEBUG -- request: Content-Type: "application/json" ... User-Agent: "Faraday v0.15.4" I, [2020-05-12T10:19:32.698568 #86437] INFO -- response: Status 422 D, [2020-05-12T10:19:32.698731 #86437] DEBUG -- response: server: "nginx/1.10.1" date: "Tue, 12 May 2020 02:19:32 GMT" content-type: "application/json; charset=utf-8" content-length: "159" connection: "close" ... ###############____response__#<Faraday::Response:0x00007f942fcd7330> ###############____response_status_422 ###############____response_body_[{"fieldNames":["Type"],"classification":"RequiredError","message":"Required"},{"fieldNames":["Config"],"classification":"RequiredError","message":"Required"}] ... ``` I had changed `hook_params.type` to `hook_params.Type`, and the same to `hook_params.Config`, but it seems do not work also. Is my `hook_params` wrong? and how to fix `hook_params`? TKS~
GiteaMirror added the issue/stale label 2025-11-02 06:23:09 -06:00
Author
Owner

@Jaskaranbir commented on GitHub (Jun 29, 2020):

A few potential causes for problems:

  • Malformed JSON. That doesnt look like valid JSON which you posted in first snippet. But I am guessing its being converted to one before being sent-off.
  • Content-Type header mismatch. Ensure that the Content-Type header is present and has value application/json. I made this mistake where my header was application/x-www-url-form-encoded, and it took me longer than it should have to realize that.
  • I noticed that in your payload, under config.content_type, you have application/json. That refers to content_type of Webhook, not your request. The only valid content-types for Webhooks are json or form.

If it helps, you can try tracing the errors you get through source-code and checking the conditions which cause them. For example, here is where the possible Webhook-types are defined: 1dd3f19ee3/models/webhook.go (L31-L34)

And if it helps, here's a sample curl-request which should work:

curl --location --request POST 'http://git-tea-host/api/v1/repos/test-user/test-repo/hooks' \
--header 'Content-Type: application/json' \
--header 'Authorization: Basic <someBase64Str>' \
--header 'Content-Type: application/json' \
--data-raw '{
  "active": true,
  "branch_filter": "*",
  "config": {
    "content_type": "json",
    "url": "http://repositories/2222",
    "http_method": "post"
  },
  "events": [
    "push_only"
  ],
  "type": "gitea"
}'
@Jaskaranbir commented on GitHub (Jun 29, 2020): A few potential causes for problems: * Malformed JSON. That doesnt look like valid JSON which you posted in first snippet. But I am guessing its being converted to one before being sent-off. * `Content-Type` header mismatch. Ensure that the `Content-Type` header is present and has value `application/json`. I made this mistake where my header was `application/x-www-url-form-encoded`, and it took me longer than it should have to realize that. * I noticed that in your payload, under `config.content_type`, you have `application/json`. That refers to `content_type` of Webhook, not your request. The only valid content-types for Webhooks are `json` or `form`. If it helps, you can try tracing the errors you get through source-code and checking the conditions which cause them. For example, here is where the possible Webhook-types are defined: https://github.com/go-gitea/gitea/blob/1dd3f19ee381d3a3554828a37282fd75ba3073ee/models/webhook.go#L31-L34 And if it helps, here's a sample curl-request which should work: ```bash curl --location --request POST 'http://git-tea-host/api/v1/repos/test-user/test-repo/hooks' \ --header 'Content-Type: application/json' \ --header 'Authorization: Basic <someBase64Str>' \ --header 'Content-Type: application/json' \ --data-raw '{ "active": true, "branch_filter": "*", "config": { "content_type": "json", "url": "http://repositories/2222", "http_method": "post" }, "events": [ "push_only" ], "type": "gitea" }' ```
Author
Owner

@Sylor-huang commented on GitHub (Jun 30, 2020):

@Jaskaranbir Thanks very much , I will try it later, whether it work or not , I will replay.

@Sylor-huang commented on GitHub (Jun 30, 2020): @Jaskaranbir Thanks very much , I will try it later, whether it work or not , I will replay.
Author
Owner

@stale[bot] commented on GitHub (Aug 29, 2020):

This issue has been automatically marked as stale because it has not had recent activity. I am here to help clear issues left open even if solved or waiting for more insight. This issue will be closed if no further activity occurs during the next 2 weeks. If the issue is still valid just add a comment to keep it alive. Thank you for your contributions.

@stale[bot] commented on GitHub (Aug 29, 2020): This issue has been automatically marked as stale because it has not had recent activity. I am here to help clear issues left open even if solved or waiting for more insight. This issue will be closed if no further activity occurs during the next 2 weeks. If the issue is still valid just add a comment to keep it alive. Thank you for your contributions.
Author
Owner

@stale[bot] commented on GitHub (Sep 12, 2020):

This issue has been automatically closed because of inactivity. You can re-open it if needed.

@stale[bot] commented on GitHub (Sep 12, 2020): This issue has been automatically closed because of inactivity. You can re-open it if needed.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/gitea#5384