使用邮递员 collection 连接到 Rest API
Connect to a Rest API, using a postman collection
我正在尝试连接到我有邮递员的 API Collection json:
{
"info": {
"_postman_id": "----",
"name": "Public API",
"description": "API",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
{
"name": "Add Bim Info for a building",
"request": {
"auth": {
"type": "basic",
"basic": [
{
"key": "password",
"value": "somepassword",
"type": "string"
},
{
"key": "username",
"value": "someusername",
"type": "string"
}
]
},
"method": "POST",
"header": [],
"body": {
"mode": "formdata",
"formdata": [
{
"key": "file",
"type": "file",
"src": []
}
]
},
"url": {
"raw": "http://00.00.00.00:0000/api/apiName/building/",
"protocol": "http",
"host": [
"00",
"00",
"00",
"00"
],
"port": "0000",
"path": [
"api",
"apiName",
"building",
""
]
},
"description": "description"
},
"response": []
}
]
当我将这个 json 加载到 postman
时,我可以毫无问题地发送文件,但是我必须实现一个控制台程序才能做到这一点,所以我尝试翻译邮递员 collection 到 c# 代码使用 RestSharp
:
string filePath = @"path_to_file";
string url = "http://00.00.00.00:0000/api/apiName/building/";
string usr = "someuser";
string pwd = "somepassword";
RestClient client = new
RestClient(url);
client.Authenticator = new HttpBasicAuthenticator(usr, pwd);
RestRequest request = new RestRequest(url);
request.Method = Method.POST;
request.AddFile(Path.GetFileNameWithoutExtension(filePath), filePath);
try
{
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
Console.ReadLine();
身份验证器工作正常,但是当我发送请求时,我收到消息:
"Required request part 'file' is not present"
如何添加此请求?
我可以使用 json 作为一些模板来发送我的请求吗?
问题已经得到解答,感谢@DavidG的评论,我把答案写成官方的。
RestRequest request = new RestRequest(url);
request.Method = Method.POST;
request.AddFile("file", filePath); //Changing this line generates the body in the json
我正在尝试连接到我有邮递员的 API Collection json:
{
"info": {
"_postman_id": "----",
"name": "Public API",
"description": "API",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
{
"name": "Add Bim Info for a building",
"request": {
"auth": {
"type": "basic",
"basic": [
{
"key": "password",
"value": "somepassword",
"type": "string"
},
{
"key": "username",
"value": "someusername",
"type": "string"
}
]
},
"method": "POST",
"header": [],
"body": {
"mode": "formdata",
"formdata": [
{
"key": "file",
"type": "file",
"src": []
}
]
},
"url": {
"raw": "http://00.00.00.00:0000/api/apiName/building/",
"protocol": "http",
"host": [
"00",
"00",
"00",
"00"
],
"port": "0000",
"path": [
"api",
"apiName",
"building",
""
]
},
"description": "description"
},
"response": []
}
]
当我将这个 json 加载到 postman
时,我可以毫无问题地发送文件,但是我必须实现一个控制台程序才能做到这一点,所以我尝试翻译邮递员 collection 到 c# 代码使用 RestSharp
:
string filePath = @"path_to_file";
string url = "http://00.00.00.00:0000/api/apiName/building/";
string usr = "someuser";
string pwd = "somepassword";
RestClient client = new
RestClient(url);
client.Authenticator = new HttpBasicAuthenticator(usr, pwd);
RestRequest request = new RestRequest(url);
request.Method = Method.POST;
request.AddFile(Path.GetFileNameWithoutExtension(filePath), filePath);
try
{
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
Console.ReadLine();
身份验证器工作正常,但是当我发送请求时,我收到消息: "Required request part 'file' is not present"
如何添加此请求? 我可以使用 json 作为一些模板来发送我的请求吗?
问题已经得到解答,感谢@DavidG的评论,我把答案写成官方的。
RestRequest request = new RestRequest(url);
request.Method = Method.POST;
request.AddFile("file", filePath); //Changing this line generates the body in the json