简体   繁体   中英

RestSharp: how to pass a variable to a function

I am using the RestSharp library for C#. I am using the POST method.

Client C#

        var client = new RestClient();
        client.BaseUrl = new Uri("http://localhost:5000");

        var request = new RestRequest(Method.POST);
        IRestResponse response = client.Execute(request);

Server Python

from flask import Flask, jsonify
import MyIotHub

app = Flask(__name__)

@app.route('/', methods=['POST'])

def iot_message():

    message = "null"
    newHub = MyIotHub()

    newHub.send(message)

if __name__ == '__main__':
    app.run(debug=True)

How can I use my iot_message function specifically? And how should I pass a variable to this function?

What you call on your service is decided by the resource your request is set to ie

request.Resource = "your/endpoint/here"

Which in your case looks to be the root.


If you want to POST data to this endpoint, you can attach a parameter using

request.AddParameter("valuename", "value");

And access that on the form of your webservice by doing

def iot_message()
     whatever = request.form['valuename']

If you want that to be accessible from the URL instead, use

def iot_message()
    whatever = request.args.get('valuename')

And

request.AddParameter("valuename", "value", ParameterType.QueryString);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM