Home > Enterprise >  How to make post call if I have JSON in django
How to make post call if I have JSON in django

Time:09-23

I am working on django project where I have JSON data for Post call request to trigger the post function. here's my view

def jiranewsprint(request):
    default_node = list(Lookup.objects.values('Name').filter(Type = 'JENKINS_NODES', default_node = 'Default').values())[0].get('Name')
    print('default_node',default_node)
    jextract= request.body
    print(jextract)
    jextract = json.loads(jextract)
    jiraSprint = jextract["issue"]["fields"]["customfield_10200"]
    print('jiraSprint',jiraSprint)
    sandboxcall= {
    "id": 18,
    "robot": "Sandbox_Creation_Bot",
    "param": [
        {
        "node": default_node,
        "Username": "hello",
        "Password": "hello@21",
        "WebsiteURL": "https//:google.com",
        "SandboxName": jiraSprint '_sandbox',
        "Publishable": "Yes",
        "Description": "testing",
        "Tools": "Application Composer"
        }
        ]
    }
    print(sandboxcall)
    return HttpResponse(status=200) 

Need help that how make post call with the json request I have ?

CodePudding user response:

You function should look like this:

import json
import requests


def jiranewsprint(request):
    default_node = list(Lookup.objects.values('Name').filter(Type = 'JENKINS_NODES', default_node = 'Default').values())[0].get('Name')
    print('default_node',default_node)
    jextract= request.body
    print(jextract)
    jextract = json.loads(jextract)
    jiraSprint = jextract["issue"]["fields"]["customfield_10200"]
    print('jiraSprint',jiraSprint)
    sandboxcall= {
        "id": 18,
        "robot": "Sandbox_Creation_Bot",
        "param": [
            {
            "node": default_node,
            "Username": "hello",
            "Password": "hello@21",
            "WebsiteURL": "https//:google.com",
            "SandboxName": jiraSprint '_sandbox',
            "Publishable": "Yes",
            "Description": "testing",
            "Tools": "Application Composer"
            }
        ]
    }
    print(sandboxcall)

    # construct headers if required
    headers = {
        'Content-type': 'application/json',
        'Authorization': 'auth_token',
    }
    url = 'your-post-url'

    res_data = requests.request('POST', url=url, data=json.dumps(sandboxcall), headers=headers).json()

    return HttpResponse(status=200)
  • Related