如何使用python或pycurl发布表单数据时从文件中获取值

时间:2018-01-16 21:58:58

标签: python pycurl

我的一篇文章中包含了smaple.txt 1234 4567 7894 这些值应该传递给pycurl中的表单数据。 我试过这种方式,但它给了我一个错误:

import pycurl
import os
try:
    # python 3
     from urllib.parse import urlencode
except ImportError:
    # python 2
    from urllib import urlencode

class FileReader:
    def __init__(self, fp):
         self.fp = fp
    def read_callback(self, size):
       return self.fp.read(size)
c = pycurl.Curl()
filesize = os.path.getsize('smaple.txt')
with open('smaple.txt', 'r') as f:
    c.setopt(pycurl.POSTFIELDSIZE, filesize)
    c.setopt(pycurl.READFUNCTION, FileReader(f).read_callback)
    for line in f:
        print(line)
        c.setopt(c.URL, 'http://app.spongecell.com/api/queries')
        c.setopt(pycurl.HTTPHEADER, ['content-type: multipart/form-data'])
        post_data = {'user_credentials': 'xxxxxxx','query':'{campaign(id:line){name,status }}'}
# Form data must be provided already urlencoded.
            postfields = urlencode(post_data)
# Sets request method to POST,
# Content-Type header to application/x-www-form-urlencoded
# and data to send in request body.
            c.setopt(c.POSTFIELDS, postfields)
            c.perform()
            c.close()
  

错误:{"错误":[{"消息":"参数' id'在Field' campaign'具有无效值。预期类型' ID!'。","位置":[{"行":1,"列":2 }],"字段":["查询""运动"" ID"]}]}

有人可以帮忙吗?因为我是python的新手。

0 个答案:

没有答案