简体   繁体   English

通过jenkins REST API创建作业会产生403禁止错误

[英]Creating a job through jenkins REST API gives 403 forbidden error

I am trying to create a job through jenkins REST API in Python, but i am getting the 403 error that says it is forbidden for the url. 我正在尝试通过Python中的jenkins REST API创建工作,但我收到403错误,该错误表示该URL被禁止。 I am passing a config.xml file to the REST API along with the job name as parameter. 我将config.xml文件以及作业名称作为参数传递给REST API。 I tied all the solutions that i could find, but nothing works. 我捆绑了所有可以找到的解决方案,但没有任何效果。 There is no CSRF protection enabled for my jenkins instance and i am not sure what else is the problem 我的jenkins实例未启用CSRF保护,我不确定还有什么问题

I am posting my code and error here. 我在这里发布我的代码和错误。

This is my code 这是我的代码

This is my post method 这是我的post方式

```def post(self, endpoint, **kwargs):
   kwargs.update(self.kwargs.copy())
   kwargs["headers"].update(
       {"Content-Type": "text/xml"})
   url = self.make_url(endpoint)
   log.info("Request url: {}".format(url))
   log.info("Send HTTP POST request {} with kwargs "
            "{}".format(url, str(kwargs)))
   response = requests.post(url, **kwargs)

def verify_jenkins_job_creation(self, config_file):
    job_name = "NewJob"
    endpoint = "/createItem"
    data = config_file
    headers = {
        "Content-Type": "test/xml"
    }
    print(headers)
    params = (
        ('name', 'NewJob'),
    )
    response = self.post(endpoint, data=data, params=params, headers=headers)
    log.info("Url response: {}".format(response))
    return response





This is the error that i am getting
>           raise HTTPError(http_error_msg, response=self)
E           requests.exceptions.HTTPError: 403 Client Error: Forbidden for url: https://jenkins-server/createItem?name=NewJob

This is what i am seeing in my debug logs
jenkins_rest.py            165 INFO     Request url: https://jenkins/createItem
jenkins_rest.py            167 INFO     Send HTTP POST request https://jenkins-/createItem with kwargs {'verify': False, 'headers': {'Accept-Encoding': 'gzip', 'Content-Type': 'text/xml', 'Accept': 'application/json'}, 'params': (('name', 'NewJob'),), 'data': '/home/user/config.xml', 'auth': <requests.auth.HTTPDigestAuth object at 0x7f71ea0de908>}
connectionpool.py          758 INFO     Starting new HTTPS connection (1): jenkins server
connectionpool.py          387 DEBUG    "POST /createItem?name=NewJob HTTP/1.1" 403 None

The config.xml file has the content that i copied from one already existing jenkins job through https://jenkins-instance/job/test_job/config.xml

It sounds like you are using wrong context root to POST your request. 听起来您使用错误的上下文根来发布您的请求。

If your sample job is : "https://jenkins-instance/job/test_job/config.xml" Then you should "POST@https://jenkins-instance/job/createItem=?NewJob 如果您的示例作业是: "https://jenkins-instance/job/test_job/config.xml"那么您应该"POST@https://jenkins-instance/job/createItem=?NewJob

Regards 问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM