zoukankan      html  css  js  c++  java
  • WebApi 之请求参数

    在学习webApi请求过程中,发现参数的传递是一个比较重要的过程,初学习时,感觉自己弄懂了,然后测试好像ok。但换了一个环境后,又发现自己蒙了。故而想到整理下这其中的请求情况。现在列出一些表格如下:

    [HttpGet]

    Type

     ajax request Data

    request contentType 

    api parameter 

    remarks 

     get   primary type-->multiply parameter:data:{id:1,name:'zhaosw'}-->Json object  none  get(int id,string name) the samplest request for ajax
     get object type-->one parameter:data:{id:2,name:'zhaosw',age:'25'}-->Json object  none  get([fromUri] User user)  request for object
     get  object type-->one parameter:data:{strQuery:JSON.stringify({id:3,name:'zhaosw'})}-->Json string  contentType:'application/json'  get(string strQuery)  request for object string.need to JsonConvert.DeserializeObject()
     get not to request       request for Array
     get  tips     should add [httpget] when the method name does't start with 'get'

    [HttpPost]

    Type ajax request Data request contentType api parameter remarks
    post primary type-->one parameter:data:{'':1}. tips:not key but value. none  post([fromBody]int id) request for primary type with one param
    post priamry type-->multiply parameter:data:JSON.stringify({id:2,name:'zhaosw',age:25}) contentType:'application/json'

    post(dynamic obj) 

    string name=Convert.toString(obj.name)

    request for priamry type with multiply  param
    post object type-->one parameter:data:{id:3,name:'zhaosw',age:25} none post(User user). User defined already. request for object type
    post object type->one parameter:data:JSON.stringify({id:4,name:'zhaosw'}) contentType:'application/json' post(User user). User defined already. request for object type
    post parameter and object type-->one paramter:data:JSON.stringify({no:123,user:{id:1,name;"zhaosw,age:25}}) contentType:'application/json'

    post(dynamic obj). 

    var no=Convert.toStriong(obj.no)

    var user=JsonConvert.DeserializeObject(Convert.ToString(obj.user))

    request for complex param
    post object type(Array with primary type)-->one parameter:data:JSON.stringify(['1','2','3','4']) contentType:'application/json' post(string[] ids) request for array with primary type
    post object type(Array with primary type)-->one parameter:data:JSON.stringify([{id:1,name:'zhao',age:12},{id:2,name:'sw',age:13},{id:3,name:'zhaosw',age:25}]) contentType:'application/json' post(list userlist) request for array with primary type
    post

    all above request with ajax.

    next for end  request for api.

    that is webrequest and webresponse more info you can search the internet or below.

         

    [HttpPut]

    the same with [HttpPost] .refer to [HttpPost]

    [HttpDelete]

    the same with [HttpPost] .refer to [HttpPost]

    重要的链接:

    1.webapi 参数传递(上)

    2.webapi 参数传递(下)

    同时要感谢该作者:赖的安分的博文。

  • 相关阅读:
    SpringCloud微服务Zuul跨域问题
    com.netflix.zuul.exception.ZuulException: Hystrix Readed time out
    Java实现遍历N级树形目录结构
    ubuntu安装Nginx
    redis报错:java.net.SocketException: Broken pipe (Write failed); nested exception is redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketException: Broken pipe (Write failed)
    Java设置接口跨域
    SpringBoot使用qq邮箱发送邮件
    linux使用Nginx搭建静态资源服务器
    Spring Boot 正常启动后访问Controller提示404
    分享2019年陆陆续续读过的书-附书单
  • 原文地址:https://www.cnblogs.com/zhaosw/p/7481409.html
Copyright © 2011-2022 走看看