如何使用REST编写用于文件上传的Python代码?

时间:2019-04-19 09:54:47

标签: python python-2.7 rest curl multipartform-data

我有以下curl命令,为此我需要编写一个方法。 CURL命令:

import java.awt.*;
import javax.swing.*;

public class GameView extends JFrame {
    private JPanel buttonPanel;
    private JPanel infoPanel;
    private JTextArea informationDisplay;
    private JTextArea availableUnits;
    private JPanel unitButtonPanel;
    private JPanel disasterPanel;
    private JTextArea disasterDisplay;
    private JButton ambulanceUnit;
    private JButton diseaseControlUnit;
    private JButton gasControlUnit;
    private JButton fireTruck;

    public static void main(String[] args) {
        new GameView();
    }

    public GameView() {
        setSize(1000, 500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Toolkit tk = Toolkit.getDefaultToolkit();
        Dimension dim = tk.getScreenSize();
        int xPos = (dim.width/2) - (this.getWidth()/2);
        int yPos = (dim.height/2) - (this.getHeight()/2);
        this.setLocation(xPos,yPos);

        JPanel title = new JPanel();
        JLabel label1 = new JLabel("Invaded City");
        title.add(label1);
        add(title, BorderLayout.NORTH);

        buttonPanel = new JPanel();
        buttonPanel.setLayout(new GridLayout(10, 10, 2, 2));
        for (int i = 0; i < 100; i++) {
            JButton mapButton = new JButton();
            addButton(mapButton);
        }
        add(buttonPanel, BorderLayout.CENTER);  

        JPanel textPanel = new JPanel();
        textPanel.setLayout(new GridLayout(3, 1, 4, 4));

        availableUnits = new JTextArea(2,2);
        //availableUnits.setPreferredSize(new Dimension(200, 200));

        unitButtonPanel = new JPanel();
        unitButtonPanel.setLayout(new GridLayout(2, 2, 2, 2));
        ambulanceUnit = new JButton("Ambulance");
        diseaseControlUnit = new JButton ("diseaseControlUnit");
        gasControlUnit = new JButton("gasControlUnit");
        fireTruck = new JButton("fireTruck");
        unitButtonPanel.add(ambulanceUnit);
        unitButtonPanel.add(diseaseControlUnit);
        unitButtonPanel.add(gasControlUnit);
        unitButtonPanel.add(fireTruck);
        availableUnits.add(unitButtonPanel);
        textPanel.add(availableUnits);

        JTextArea respondingUnits = new JTextArea(2,2);
        //respondingUnits.setPreferredSize(new Dimension(200, 200));
        textPanel.add(respondingUnits);

        JTextArea treatingUnits = new JTextArea(2,2);
        //treatingUnits.setPreferredSize(new Dimension(200, 200));
        textPanel.add(treatingUnits);

        add(textPanel, BorderLayout.EAST);

        JPanel infoPanel = new JPanel();
        infoPanel.setLayout(new BorderLayout());
        JTextArea informationDisplay= new JTextArea();
        JScrollPane scrollbar1 = new JScrollPane(informationDisplay, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scrollbar1.setSize( 20, 20 );
        informationDisplay.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 20));
        informationDisplay.setText("The \n" + "It\n "+ "The\n" +"A\n" + "About\n" + "\n" + "Cats\n" + "Cats\n" + "Cats\n" + "Cats\n");
        informationDisplay.setPreferredSize(new Dimension(getWidth(), 200));

        infoPanel.add(scrollbar1);
        add(infoPanel, BorderLayout.SOUTH);

        disasterPanel = new JPanel();
        disasterPanel.setLayout(new BorderLayout());
        disasterDisplay = new JTextArea();
        disasterDisplay.setPreferredSize(new Dimension(getWidth(), 200));

        disasterPanel.add(disasterDisplay);
        add(disasterPanel, BorderLayout.WEST);

        pack();
        setVisible(true);
    }
    public void addButton(JButton b) {
        buttonPanel.add(b);
    }
}

我已经为下面的方法编写了以下方法,但是它失败了。 代码:

[
  {"from":"",
    "op":0,
      "path":"/projectEntitlements",
        "value":{"projectRef":{"id":"<MyProjectID>"},"group":{"groupType":3}}

  }
]

我在上面的代码中得到的REST响应是:

  

提供的类ABC没有可以反序列化JSON对象的JsonObject构造函数或带有@ConstructorProperties注释的构造函数

我还尝试了使用以下代码更改进行文件打开操作:

curl -X POST "https://example.com:8443/api/rest/abc_service/123/upload/passwd" -H "accept: application/json" -H "Content-Type: multipart/form-data" -F "body=@abhifile.txt;type=text/plain"

在上述情况下,打开文件时出现错误:

  

ValueError:对已关闭文件的I / O操作

1 个答案:

答案 0 :(得分:0)

您正在通过键filename传递字符串,而不是传递文件的内容。

def upload_passwd(self):
    files = {'filename': open('/root/Desktop/vdm/abhifile.txt','rb')}
    header = {
    'Content-Type': "multipart/form-data"
    }
    self._request.headers.update(header)
    response = self._request.post(operation='upload/passwd', 
    object_id=self.object_id, files=files)