社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

这个python servicenow脚本的Perl版本发布了一个文件

user1428649 • 6 年前 • 2115 次点击  

所以我现在要查看ServiceNow的附件API,文档如下: https://docs.servicenow.com/integrate/inbound_rest/reference/r_AttachmentAPI-POST.html

对于我正在开发的应用程序,我需要编写一些Perl代码来处理附件。不幸的是,ServiceNow Perl API库不能处理大于5mb的附件,因此我需要使用实例附带的stock附件API。

在上面的链接中,我看到了一个关于如何用这个python代码发布文件的示例。

#Need to install requests package for python
#easy_install requests
import requests

# Set the request parameters
url = 'https://instance.service-now.com/api/now/attachment/file?table_name=incident&table_sys_id=d71f7935c0a8016700802b64c67c11c6&file_name=Issue_screenshot.jpg'

# Specify the file To send. When specifying fles to send make sure you specify the path to the file, in
# this example the file was located in the same directory as the python script being executed.
data = open('Issue_screenshot.jpg', 'rb').read()

# Eg. User name="admin", Password="admin" for this code sample.
user = 'admin'
pwd = 'admin'

# Set proper headers
headers = {"Content-Type":"image/jpeg","Accept":"application/json"}

# Do the HTTP request
response = requests.post(url, auth=(user, pwd), headers=headers, data=data)

# Check for HTTP codes other than 201
if response.status_code != 201: 
    print('Status:', response.status_code, 'Headers:', response.headers, 'Error Response:',response.json())
    exit()

# Decode the JSON response into a dictionary and use the data
data = response.json()
print(data)

我经常使用REST::Client来发帖,但是不幸的是,我找不到一个很好的例子来说明如何处理上面的^^^但是在Perl中。如何使用REST::Client来发布上述文件?

我通过调用 curl 在我的脚本中,但是使用REST::Client会更优雅。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/52264
文章 [ 1 ]  |  最新文章 6 年前