从Salesforce端点URL获得响应

问题描述 投票:0回答:1

我的老板要我编写一个脚本来监视Enpoint Url的脚本。通常会得到回应。在这种情况下200将成功响应。

这里salesforce测试组织具有其唯一API_Key值的多种服务。我需要在标头中传递带有API_Key的URL以获得成功的响应。

我正在使用SOQL查询从组织中获取数据。考虑以下endpointurl.bat脚本

echo off

echo '======================= Login to SF ==============================' &

sfdx force:auth:web:login -r https://test.salesforce.com -a USR & ^
sfdx force:data:soql:query -q "SELECT Name,Client_ID__c,Endpoint_URL__C FROM WebServices_CS__c" -u USR -r csv > input.csv & ^
python ValidateEndPoint.py

当我在.bat上面运行时,脚本会给我一个input.csv文件,其中包含所有Client_ID__c,Endpoint_URL__C来自WebServices_CS__c,然后它将重定向到另一个Python脚本,即ValidateEndPoint.py。

input.csv文件包含Column1: Name, Column2: Client_ID, Column3: Endpoint_URL__c

我已经编写了一个python脚本,它将遍历所有列这是python脚本

import pandas
import xlrd
import sys
import os.path
from os import path
import requests
import json

if path.exists('input.csv') == True:
    csvData = pandas.read_csv('input.csv')
    with open('ServiceURLResponse.json', 'w') as outfile:
        for index,row in csvData.iterrows():
            print('####################           New Item        ')
            if not row['Client_ID__c']:
                headers = {'X-API-TOKEN': row['Client_ID__c']}
                resData = requests.get(row['Endpoint_URL__c'],headers=headers)
                response_dict = resData.json()
            else:
                resData = requests.get(row['Endpoint_URL__c'])
                response_dict = resData.json()
            print(response_dict[0]['message'])
            if response_dict[0]['message'] == "HTTP Method 'GET' not allowed. Allowed are POST":
                print("Request has to be POST")
                print(row['Endpoint_URL__c'])
                if not row['Client_ID__c']:
                    print('In IF')
                    headers = {'X-API-TOKEN': row['Client_ID__c']}
                    resData = requests.post(row['Endpoint_URL__c'],headers=headers)
                else:
                    print('In Else')
                    resData = requests.post(row['Endpoint_URL__c'])

            dataLoad = {
                row['Name']:{'status':resData.status_code,'response':resData.json()}
            }
            outfile.write(json.dumps(dataLoad))

            print('###################          POST Reponse            ######################')
else:
    print('CSV File not generated')
    #print(d.headers, d.status_code)

Python脚本抛出以下错误

PS G:\Endpoint URL> py.exe .\ValidateEndPoint.py
####################           New Item
Traceback (most recent call last):
  File ".\ValidateEndPoint.py", line 41, in <module>
    response_dict = resData.json()
  File "C:\Users\Sameer\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\models.py", line 898, in json
    return complexjson.loads(self.text, **kwargs)
  File "C:\Users\Sameer\AppData\Local\Programs\Python\Python37-32\lib\site-packages\simplejson\__init__.py", line 525, in loads
    return _default_decoder.decode(s)
  File "C:\Users\Sameer\AppData\Local\Programs\Python\Python37-32\lib\site-packages\simplejson\decoder.py", line 370, in decode
    obj, end = self.raw_decode(s)
  File "C:\Users\Sameer\AppData\Local\Programs\Python\Python37-32\lib\site-packages\simplejson\decoder.py", line 400, in raw_decode
    return self.scan_once(s, idx=_w(s, idx).end())
simplejson.errors.JSONDecodeError: Expecting value: line 2 column 1 (char 2)

我不确定我要去哪里错。有谁可以帮助我更正我的脚本。也请建议我是否有其他替代或简便的方法有效地完成此任务。

python shell api web-services salesforce
1个回答
0
投票

您是否已验证endpointurl.bat文件的结果?我希望该查询在Endpoint_URL__C上失败-如果它是一个自定义字段,则应该是Endpoint_URL__c,并带有小写的'c'。

© www.soinside.com 2019 - 2024. All rights reserved.