简体   繁体   中英

Missing header error with Python requests but header defined

I am trying to create an ansible module using the python requests library, and the module runs without errors but returns the response

"{\\n \\"code\\" : \\"generic_err_missing_required_header\\",\\n \\"message\\" : \\"Missing header: [X-chkp-sid]\\"\\n}"

even though the header appears to be correctly defined.

Module code below:

from ansible.module_utils.basic import AnsibleModule
import requests

def somefunction(sid):
    url = '<someurl>'

    headers = {
        'Content-Type': 'application/json',
        'X-chkp-sid': sid,
    }

    data = {
        'type': 'tag'
    }

    response = requests.post(url,headers,data,verify=False,timeout=10)

    return response.content

def main():

    module_args = dict(
        sid = dict(type='str', required=True)
    )

    result = dict(
        changed=False,
        original_message='',
        message=''
    )

    module = AnsibleModule(
    argument_spec=module_args,
    supports_check_mode=True
    )

    if module.check_mode:
        return result

    sid = module.params['sid']

    result=somefunction(sid)

    module.exit_json(somefunction_output=result)

if __name__ == '__main__':
    main()

I have gotten a successful response using the ansible uri module and identical header/body parameters. Any other ideas?

somefunction() does not return anything, therefore the return value is None .

Perhaps you wanted return requests.post(url,headers,data,verify=False,timeout=10) ?

Most arguments to requests.post() should be keyword-style args instead of plain positional args.

Try this instead:

response = requests.post(url,data=data,headers=headers,verify=False,timeout=10)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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