Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to POST a data in JSON format to Restful service

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to POST a data in JSON format to Restful service". In daily operation, I believe many people have doubts about how to POST a data in JSON format to Restful service. Xiaobian consulted all kinds of information and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the question of "how to POST a data in JSON format to Restful service". Next, please follow the editor to study!

POST a json data on the Android/Java platform:

JSONObject jsonObj = new JSONObject (); jsonObj.put ("username", username); jsonObj.put ("apikey", apikey); / / Create the POST object and add the parametersHttpPost httpPost = new HttpPost (url); StringEntity entity = new StringEntity (jsonObj.toString (), HTTP.UTF_8); entity.setContentType ("application/json"); httpPost.setEntity (entity); HttpClient client = new DefaultHttpClient (); HttpResponse response = client.execute (httpPost)

With curl, you can execute the following commands:

Curl-l-H "Content-type: application/json"-X POST-d'{"phone": "13521389587", "password": "test"} 'http://domain/apis/users.json

Use jQuery:

$.ajax ({url:url, type: "POST", data:data, contentType: "application/json; charset=utf-8", dataType: "json", success: function () {.}})

PHP is implemented in cUrl:

$data = array ("name" = > "Hagrid", "age" = > "36"); $data_string = json_encode ($data); $ch = curl_init ('http://api.local/rest/users'); curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt ($ch, CURLOPT_POSTFIELDS, $data_string); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true) Curl_setopt ($ch, CURLOPT_HTTPHEADER, array ('Content-Type: application/json',' Content-Length:'. Strlen ($data_string)); $result = curl_exec ($ch); at this point, the study on "how to POST a piece of data in JSON format to the Restful service" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report