8

में com.google.api.client.http.HttpRequest ऑब्जेक्ट का उपयोग करके POST अनुरोध भेजना मुझे निम्नलिखित संरचना के साथ POST अनुरोध भेजना है।Google API

POST https://www.googleapis.com/fusiontables/v1/tables 
    Authorization: /* auth token here */ 
    Content-Type: application/json 

    { 
    "name": "Insects", 
    "columns": [ 
    { 
     "name": "Species", 
     "type": "STRING" 
    }, 
    { 
     "name": "Elevation", 
     "type": "NUMBER" 
    }, 
    { 
     "name": "Year", 
     "type": "DATETIME" 
    } 
     ], 
    "description": "Insect Tracking Information.", 
    "isExportable": true 
    } 

मैं नीचे दिए गए कोड का उपयोग कर रहा पोस्ट अनुरोध भेजने के लिए, लेकिन मैं "400 गलत अनुरोध" के रूप में एक प्रतिक्रिया हो रही है

String PostUrl = "https://www.googleapis.com/fusiontables/v1/tables"; 
HttpRequestFactory requestFactory = HTTP_TRANSPORT.createRequestFactory(credential); 

//generate the REST based URL 
GenericUrl url = new GenericUrl(PostUrl.replaceAll(" ", "%20")); 
//make POST request 

String requestBody = "{'name': 'newIndia','columns': [{'name': 'Species','type': 'STRING'}],'description': 'Insect Tracking Information.','isExportable': true}"; 

HttpRequest request = requestFactory.buildPostRequest(url, ByteArrayContent.fromString(null, requestBody)); 
request.getHeaders().setContentType("application/json"); 
// Google servers will fail to process a POST/PUT/PATCH unless the Content-Length 
// header >= 1 
//request.setAllowEmptyContent(false); 
System.out.println("HttpRequest request" + request); 
HttpResponse response = request.execute(); 

मुझे आश्चर्य है अगर ऐसा कोई है जो इस समान कार्य पर काम किया है मदद कर सकता है मुझे इस प्रश्न की शुरुआत में उल्लिखित POST अनुरोध प्रारूप के अनुसार POST अनुरोध भेजना है।

उत्तर

9

मैं नीचे दिए गए कोड

String requestBody = "{'name': 'newIndia','columns': [{'name': 'Species','type': 'STRING'}],'description': 'Insect Tracking Information.','isExportable': true}"; 
HttpRequest request = requestFactory.buildPostRequest(url, ByteArrayContent.fromString("application/json", requestBody)); 
request.getHeaders().setContentType("application/json"); 
+0

असल में मैं आदेश में एक सही HTTP अनुरोध प्राप्त करने के लिए, 'null' साथ' ByteArrayContent.fromString' का पहला तर्क को बदलने के लिए किया था का उपयोग कर पोस्ट अनुरोध भेज दिया है। 'requestFactory.buildPostRequest (url, ByteArrayContent.fromString (शून्य, अनुरोध बॉडी));' – Davincho

+0

'आवेदन/एक्स-www-form-urlencoded' के लिए 'com.google.api.client.http.UrlEncodedContent' का उपयोग करें' – Kalem