使用VsCode的Rest Client進行請求測試


平時工作使用VSCode進行request的提交和測試 =>{按照Rest Client 可以很輕松的幫助我們完成代碼的調試,而且能幫我們編譯成各種語言的代碼使用(Generate Code Snippet)}

如下表:我是用了Get請求,然后Shift+Ctrl+P進入VsCode的命令行,然后選擇 Rest Client:Generate Code Snippet 就會展示一下界面,然后選擇你想要轉換的語言就可以了

 

 

 

 

先把請求的代碼如下:

//正常Get請求
GET https://example.com/comments/1

//Post請求 Json格式提交
POST https://example.com/comments  
User-Agent: rest-client
Accept-Language: en-GB,en-US;q=0.8,en;q=0.6,zh-CN;q=0.4
Content-Type: application/json

{
    "name": "sample",
    "time": "Wed, 21 Oct 2015 18:27:50 GMT"
}

//Post請求表單提交
POST https://api.example.com/login 
Content-Type: application/x-www-form-urlencoded

name=foo
&password=bar

/*特別注意,參數和標頭必須有空一行,否則會報錯的*/

 

 下面給出轉換的示列代碼:

1.C#代碼 使用的Nuget包是RestSharp

var client = new RestClient("https://example.com/comments");
var request = new RestRequest(Method.POST);
request.AddHeader("user-agent", "rest-client");
request.AddHeader("accept-language", "en-GB,en-US;q=0.8,en;q=0.6,zh-CN;q=0.4");
request.AddParameter("undefined", "Content-Type: application/json{\"name\": \"sample\",\"time\": \"Wed, 21 Oct 2015 18:27:50 GMT\"}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);

 

 2.JavaScript代碼 Jquery

var settings = {
  "async": true,
  "crossDomain": true,
  "url": "https://example.com/comments",
  "method": "POST",
  "headers": {
    "user-agent": "rest-client",
    "accept-language": "en-GB,en-US;q=0.8,en;q=0.6,zh-CN;q=0.4"
  },
  "data": "Content-Type: application/json{\"name\": \"sample\",\"time\": \"Wed, 21 Oct 2015 18:27:50 GMT\"}"
}

$.ajax(settings).done(function (response) {
  console.log(response);
});

更多的轉換請自行測試該插件。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM