1

I want to send POST request to VNF to save Services .

Here is my code .

class APIClient:

    def __init__(self, api_type, auth=None):
      
        if api_type == 'EXTERNAL':
            self.auth_client = auth

    
    def api_call(self, http_verb, base_url, api_endpoint, headers=None, request_body=None, params=None):
       
        if headers is not None:
            headers = merge_dicts(headers, auth_header)
        else:
            headers = auth_header
        url_endpoint = base_url + api_endpoint

        request_body = json.dumps(request_body)
        if http_verb == 'POST':
            api_resp = requests.post(url_endpoint, data=request_body, headers=headers)
            return api_resp
        else:
            return False
def add_service():
    for service in service_list:
        
        dict = service.view_service()
        auth_dict = {
            'server_url': 'https://authserver.nisha.com/auth/',
            'client_id': 'vnf_api',
            'realm_name': 'nisha,
            'client_secret_key': 'abcd12345',
            'grant_type': 'client_credentials'

        }

        api_client = APIClient(api_type='EXTERNAL', auth=auth_dict)

        response = api_client.api_call(http_verb='POST',
                                       base_url='http://0.0.0.0:5054',
                                       api_endpoint='/vnf/service-management/v1/services',
                                       request_body=f'{dict}')

        print(response)
        if response.ok:
            print("success")
        else:
            print("no")

When I run this code it prints

<Response [415]>
no

All the functions in VNF side are working without issues and I have no issue with GET services api call. How to fix this ?

7
  • 1
    A 415 HTTP response code indicates an Unsupported Media Type. My Guess is that it has to do with your request headers. What does auth_header look like? Is it the same as auth_dict? Commented Mar 19, 2021 at 16:27
  • 2
    The data kwarg makes the content type form encoded, not as json. Did you mean to use the json kwarg in requests.post? Commented Mar 19, 2021 at 16:28
  • @C.Nivs Good catch Commented Mar 19, 2021 at 16:29
  • @PaulM.yes . Its a dictionary . Commented Mar 19, 2021 at 16:33
  • @C.Nivs . I didn't get you. Actually I am new to python. I want to POST the services in service list to vnf. Here service_list is a list of dictionaries contain services. Commented Mar 19, 2021 at 16:36

1 Answer 1

2

If you need to post application/json data to an endpoint, you need to use the json kwarg in requests.post rather than the data kwarg.

To show the difference between the json and data kwargs in requests.post:

import requests
from requests import Request

# This is form-encoded
r = Request('POST', 'https://myurl.com', headers={'hello': 'world'}, data={'some': 'data'})
x = r.prepare()
x.headers
# note the content-type here
{'hello': 'world', 'Content-Length': '9', 'Content-Type': 'application/x-www-form-urlencoded'}

# This is json content
r = Request('POST', 'https://myurl.com', headers={'hello': 'world'}, json={'some': 'data'})
x = r.prepare()
x.headers
{'hello': 'world', 'Content-Length': '16', 'Content-Type': 'application/json'}

So you don't need the json.dumps step here at all:

        url_endpoint = base_url + api_endpoint

        if http_verb == 'POST':
            api_resp = requests.post(url_endpoint, json=request_body, headers=headers)
Sign up to request clarification or add additional context in comments.

6 Comments

Is there any way to do this using add_service() function , without changing APIClient class ?
You need to change the add_service() function or this won't work
@Nisha sorry, my mistake, you'll need to change the class because the call to requests is wrong
After change API class , I got this issue , ``` 0.0.0.0:5054/vnf/service-management/v1/services validation error: "{'auto_recover': 0, 'description': 'description31', 'service': 'apache_server21', 'service_resources': [{'name': 'apache_script1', 'type': 'script', 'config': {'file': '/etc/init.d/apache_httpd', 'user_account': ''}}], 'snmp_messages': {'error_snmp': 'error_snmp1', 'start_snmp': 'start_snmp', 'stop_snmp': 'stop_snmp'}, 'service_id': UUID('8fe1e5bf-9150-442d-ad6d-7d16f942ecef')}" is not of type 'object' ```
@Nisha this would be a different question on SO
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.