简体   繁体   中英

Using JSON data from API GET to POST to another API via python script

So, I'm new to python and am struggling, self taught, and still learning to code. So be easy on me :)

I am using a script to get data from one source (Jira's API) and trying to use those results to post to another (PowerBi).

I've managed to successfully get the data, I just don't know how to pass the data to this other API.

I know how to use the GET and POST calls, it's just using the data from one to another than I can't seem to find anything about. Assuming since what I'm asking for is very specific?

edit: I also want to mention that while my get is asking for specific data, I'm getting more than I actually need. So I need a way to specify (hopefully) what data is actually being sent to PowerBi's API

import json
import requests

url = 'https://mydomain.atlassian.net/rest/api/2/search'

headers = { 'Content-Type' : 'application/json',
        'Authorization' : 'Basic 123456789' }

params = {
   'jql' : 'project IN (, PY, CH, NW, RP, DP, KR, DA, RE, SS, CR, CD, AB) AND issueType=incident AND statusCategory!=Done',
   'startAt': 0,
   'maxResults' : 50,
}

requestget = requests.get(url, headers=headers, params=params)

if requestget.status_code == 200:
    print(json.dumps(json.loads(requestget.text), sort_keys=True, indent=4, separators=(",", ": ")))
else:
     print("None")

Apologies if I miss understood what you were asking help on, but you could use this to send a POST request as json.

request = urllib.request.Request()#Put the powerbi api here
request.add_header('Content-Type', 'application/json; charset=utf-8')

jsondata = #your json data
jsonBytes = jsondata.encode('utf-8')

#Has to be bytes

request.add_header('Content-Length', len(jsonBytes))
response = urllib.request.urlopen(request, jsonBytes)

You could go with a requests.post instead.

jsondata = #put json data here

headers = {'content-type': 'application/json'}
response = requests.post(url, data=json.dumps(jsondata), headers=headers)

Requests documentation

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