How to post a JSON-formatted data to a restful service

Source: Internet
Author: User

To implement a post JSON data on the Android/java platform:

JsonobjectJsonobj= New Jsonobject();Jsonobj.Put("Username",Username);Jsonobj.Put("Apikey",Apikey);Create the POST object and add the parametersHttpPostHttpPost= New HttpPost(Url);StringentityEntity= New Stringentity(Jsonobj.Tostring(),HTTP.utf_8); entity.. Setentityentityhttpclient client = new  defaulthttpclient (); httpresponse response = client execute (httppost     

Use Curl to execute the following command:

Curl-l-H "Content-type:application/json"-X post-d ' {"Phone": "13521389587", "Password": "Test"} ' http://domain/apis/ Users.json

With jquery:

$ajax ({ Url:url, Type: "POST"  Data:data,: "Application/json; Charset=utf-8 ", Datatype:" JSON " , Success: function< Span class= "pun" > () {  ...  }})       /span>                

PHP is implemented with Curl:

1 $data=Array("Name" = "Hagrid", "Age" = "36"); 2 $data _string= Json_encode ($data); 3 $ch= Curl_init (' http://api.local/rest/users '); 4curl_setopt ($ch, Curlopt_customrequest, "POST"); 5curl_setopt ($ch, Curlopt_postfields,$data _string);6curl_setopt ($ch, Curlopt_returntransfer,true);7curl_setopt ($ch, Curlopt_httpheader,Array( 8' Content-type:application/json ',9' Content-length: '.strlen($data _string)) Ten );  One $result= Curl_exec ($ch);

How to post a JSON-formatted data to a restful service

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.