0

I'm trying to make a request, in python, from a VM running Ryu controller to a VM running a openvswitch. I've tested said request, and it works when I execute it in a terminal (a string is supposed to be returned):

curl -X POST -d '{"priority": 500, "match": {"in_port": 3}}' http://localhost:8080/stats/flow/8796748823560

This was my first try in python:

import subprocess

proc = subprocess.run(['curl', '-X', 'POST', '-d', '\'{"priority": 500, "match": { "in_port": 3} }\'','http://localhost:8080/stats/flow/8796748823560'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)

response = proc.stdout.decode('utf-8')

A simple POST, as you can see. However, the response is always " ", showing the error:

curl: (3) URL using bad/illegal format or missing URL

Then, I decided to use the requests python library and wrote the POST the following way:

import requests

data = '{ "priority": 500, "match": {"in_port": 3}}'

response = requests.post('http://localhost:8080/stats/flow/234', data=data)

However, I don't know where to put the option -X. In the library documentation, I cannot find the right place to put it, if there is any.

I need help to understand where I should place that -X option in the code and, if it is not possible, how could I execute that curl on python (I was trying to avoid the shell=True flag on subprocess, as I don't think it is safe).

8
  • 1
    -X POST instructs curl to make a POST request. You're already doing that in your last approach by using requests.post(). Commented Jan 2, 2020 at 0:21
  • 1
    Does this answer your question? Post JSON using Python Requests Commented Jan 2, 2020 at 0:21
  • 1
    You don't need the single quotes in your first attempt to use subprocess: subprocess.run(['curl', '-X', 'POST', '-d', '{"priority": 500, "match": { "in_port": 3} }', 'localhost:8080/stats/flow/8796748823560'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)`. They were part of the shell syntax for specifying the JSON as a single argument, not part of the JSON itself. Commented Jan 2, 2020 at 0:24
  • 1
    you can uses url httpbin.org/post with curl and python code and it will send back all data which it get. And then you can compare both to see differences. One of differences can be User-Agent header because Python sends Python/something and some servers automatically block it. Commented Jan 2, 2020 at 0:36
  • 1
    why do you use "in_port": in_port instead of "in_port": 3 ? It can makes problem. Commented Jan 2, 2020 at 0:37

1 Answer 1

1

The -X/--request in curl is an option that is followed by the HTTP verb to be used in the request. Since this is followed by POST it means that a POST request should be used. In fact -X POST is not needed since the mere presence of -d should cause curl to make a HTTP POST request.

Thus, use of request.post with data containing the body should be sufficient.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.