JSON数据到Web服务-如何定义期望的JSON数据 [英] JSON data to web service- how do define expected JSON data
问题描述
我正在用PHP构建一个RESTful Web服务,该服务接受JSON作为其有效负载 . 现在,我的问题是,如何准确地向用户描述JSON请求的格式?我是JSON的新手,但我认为我100%都不了解.
I am building a RESTful web service in PHP that accepts JSON as its payload . Now, my question is, how exactly do I describe to the user the format that the JSON request comes in? I am new to JSON and don't think I have a grasp 100%.
其他用户系统是否会基本上用我需要的键值对创建一个数据结构(如数组),将其编码为JSON并将其发送到我的Web服务?
Will the other users system basically create a data structure (such an array) with the key value pairs that I need, encode this in JSON and send it to my web service?
我真的会给他们一个数组需要看起来像什么的副本吗? 谢谢!
Do I literally give them a copy of what the array needs to look like? Thanks!
推荐答案
遵循REST的准则,您从API用户那里获得的输入应该是URL的一部分(对于GET请求;不是更新数据,即http://example.com/api/doc/1243
),或者应该是POST变量(在HTTP请求的正文中发送到同一URL).
Adhering to the guidelines of REST, the input you get from the users of the API should either be part of the URL (for a GET request; doesn't update data, i.e. http://example.com/api/doc/1243
), or should be a POST variable (sent in the body of the HTTP request, to the same URL).
因此,对于文档来说,您需要做的只是指出端点URL(http://example.com/api/doc/1243
),他们需要使用POST的事实以及变量的含义(即title
,body
,author
, 等等.).然后,用户将根据需要创建适当的HTTP请求,并且在服务器端,因为您使用的是PHP,所以将从$_POST
数组(即$_POST['title']
,$_POST['body']
等)中拉出他们的变量.
So for documentation, all you should need to do is indicate the endpoint URL (http://example.com/api/doc/1243
), the fact they need to use POST, and what the variables are (i.e. title
, body
, author
, etc.). Then users will form the proper HTTP request however they want, and on your server end, because you're using PHP, you'll pull their variables from the $_POST
array (i.e. $_POST['title']
, $_POST['body']
, etc.)
这篇关于JSON数据到Web服务-如何定义期望的JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!