Get the App
SLTechnology News&Howtos  ›  Development  › 

How to POST a data in JSON format to Restful service

Shulou Source: shulou.com Published: 2022-06-03 19:46:27 09月21日 Update

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!

Tags: Data formats services learning more help practical next commands platforms articles methods theories knowledge articles websites materials follow questions easy to use Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB MySQL macOS Redmi OPPO Reno