I'm coding a custom yet simple client to test elgg web services with Java. I want to send a post request to the server with a simple parameter but.
Here is my exposed function in elgg:
function hello_world($name) {
return "Hello ".$name;
}
elgg_ws_expose_function(
"test.echo",
"hello_world",
array("name" => array("type" => "string", "required" => true)),
'A testing method which echos back a string',
'POST',
false,
false
);
and here is my java code for sending a post request:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package readjson;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
public class Main {
public static void main(String[] args) {
try {
CloseableHttpClient client = HttpClients.createDefault();
JSONObject object = new JSONObject();
String name = "Mousa";
object.put("name", name);
HttpPost httpPost = new HttpPost("http://localhost/elgg/services/api/rest/json?method=test.echo");
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
StringEntity entity = new StringEntity(object.toJSONString());
System.out.println(object);
httpPost.setEntity(entity);
CloseableHttpResponse response = client.execute(httpPost);
System.out.println(response.getStatusLine().getStatusCode());
HttpEntity httpEntity = response.getEntity();
StringBuilder stringBuilder = new StringBuilder();
InputStream inputStream = httpEntity.getContent();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line);
}
inputStream.close();
System.out.println(stringBuilder.toString());
client.close();
}
catch(Exception ex) {
System.out.println(ex.getLocalizedMessage());
}
}
}
But I'm receiving this output and there is a problem with post request. I don't know what is wrong with that code:
{"name":"Mousa"}
200
{"status":-1,"message":"Missing parameter name in method test.echo"}
It says that "name" parameter is missing!!!
Please help
As far as I remember, Elgg's web services can't handle JSON in request body, use serialized query string instead, e.g.:
name=Mousa&interest[]=interest1&interest[]=interest2
Elgg will likely use parse_str(), so this might be helpful: http://php.net/manual/en/function.parse-str.php
Related
Edit (Answer): KEY is case sensitive. I was using Key instead of key
I am creating a simple HTTP request to google places autocomplete api but I am getting an error
https://developers.google.com/places/web-service/autocomplete
Code: 200
Response: OK
HTTP/1.1 200 OK
"error_message" : "This service requires an API key.",
"predictions" : [],
"status" : "REQUEST_DENIED"
My Key is enabled and live is related to "Google Places API Web Service".
Here is my sample code. What I am doing wrong?
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class GoogleAPI {
public static void main(String[] args) throws URISyntaxException, ClientProtocolException, IOException {
CloseableHttpClient client = HttpClients.createDefault();
URI uri = new URIBuilder().setScheme("https")
.setHost("maps.googleapis.com")
.setPath("maps/api/place/autocomplete/json")
.setParameter("input", "London")
.setParameter("Key", "*********")
.build();
HttpGet getMethod = new HttpGet(uri);
HttpResponse response = client.execute(getMethod);
System.out.println("Code: " + response.getStatusLine().getStatusCode());
System.out.println(("Response: ")+ response.getStatusLine().getReasonPhrase() );
System.out.println(response.getStatusLine().toString());
HttpEntity entity = response.getEntity();
String responseString = EntityUtils.toString(entity, "UTF-8");
System.out.println(responseString);
}
}
I have hid my key.
I have been able to successfully authentication to a service that requires ntlm authentication when using the WinHttpClients and a GET request. However when I try to do a POST I always get a 401 return code. Has anyone done this sucessfully before?
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.NTCredentials;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.WinHttpClients;
public class WindowsAuthPOst {
public static void main (String []args) throws Exception, IOException
{
org.apache.log4j.BasicConfigurator.configure();
CloseableHttpClient httpclient = WinHttpClients.createDefault();
HttpHost target = new HttpHost("SomeHost.domain", 443, "https");
HttpClientContext context = HttpClientContext.create();
HttpGet httpget = new HttpGet("/some/Service.svc");
CloseableHttpResponse response1 = httpclient.execute(target, httpget, context);
try {
HttpEntity entity1 = response1.getEntity();
} finally {
response1.close();
}
// Execute an expensive method next reusing the same context (and connection)
HttpPost httppost = new HttpPost("/some/Service.svc");
httppost.setHeader("SOAPAction", "Some Soap Action");
httppost.setEntity(new StringEntity("Soap Payload"));
CloseableHttpResponse response2 = httpclient.execute(target, httppost, context);
try {
HttpEntity entity2 = response2.getEntity();
} finally {
response2.close();
}
}
}
You can check if it is available with.
if (!WinHttpClients.isWinAuthAvailable()) {
System.out.println("Integrated Win auth is not supported!!!");
}
If not, it could be that you do not have jna.jar in your classpath. It depends on jna and will silently return false on the above if it not there, see source code.
Try with get (or options) before post. Some webservers requires that because of CORS.
https://stackoverflow.com/a/38410411/2376661
This is the code that worked for me to consume a webservice say for example "urn:authe#createEmp". Now in the same server when i consume
"urn:authe#createUser" it throws java.util.zip.ZipException . Following is the program.
package pack;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.HttpClientBuilder;
public class WebServiceClient {
public static void main(String[] args) throws ClientProtocolException, IOException {
String url = "http://localhost/ProjectName/WebService.php";
HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(url);
post.setHeader("SOAPAction", "urn:authe#createUser");
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addTextBody("firstname", "Siva", ContentType.TEXT_PLAIN);
builder.addTextBody("lastname", "LASTNAME", ContentType.TEXT_PLAIN);
// builder.addBinaryBody("file", new
// File("..."),ContentType.APPLICATION_OCTET_STREAM, "file.ext");
HttpEntity multipart = builder.build();
post.setEntity(multipart);
HttpResponse response = client.execute(post);
System.out.println("Response Code : " + response.getStatusLine().getStatusCode());
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
System.out.println(result.toString());
}
}
Response from console is:
Response Code : 200
Exception in thread "main" java.util.zip.ZipException: Not in GZIP format
at java.util.zip.GZIPInputStream.readHeader(GZIPInputStream.java:164)
at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:78)
at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:90)
at org.apache.http.client.protocol.ResponseContentEncoding$1.create(ResponseContentEncoding.java:67)
at org.apache.http.client.entity.LazyDecompressingInputStream.initWrapper(LazyDecompressingInputStream.java:54)
at org.apache.http.client.entity.LazyDecompressingInputStream.read(LazyDecompressingInputStream.java:72)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:154)
at java.io.BufferedReader.readLine(BufferedReader.java:317)
at java.io.BufferedReader.readLine(BufferedReader.java:382)
at pack.WebServiceClient.main(WebServiceClient.java:78)
I tried searching for options to encode the request with gzip format. till now got no solution. Help me regarding this.
There is a json file on my websites floder.
Here is the content:
{
"IsUpdateForcibly": "false",
"Version": "1.0",
"ReleaseNote": "OHOHOHOHOHO",
"DownloadLink": "http://192.168.1.37:11604/APK/FrauleinProject.apk"
}
If I use the browser to see,like http://localhost:11604/Content/CheckVersion.json, the result is same as thefile's content.
While I use the Java code. the response content is a little bit different.
?{
"IsUpdateForcibly": "false",
"Version": "1.0",
"ReleaseNote": "OHOHOHOHOHO",
"DownloadLink": "http://192.168.1.37:11604/APK/FrauleinProject.apk"
}
Why there is a question mark in the front of the string?
Here is is my httpclient code.
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import sun.misc.IOUtils;
import sun.net.www.http.HttpClient;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.client.WinHttpClients;
import org.apache.http.util.EntityUtils;
public class DesUtil {
public static void main(String[] args) throws Exception {
CloseableHttpClient httpclient = WinHttpClients.createDefault();
// There is no need to provide user credentials
// HttpClient will attempt to access current user security context through
// Windows platform specific methods via JNI.
try {
HttpGet httpget = new HttpGet("http://localhost:11604/Content/CheckVersion.json");
System.out.println("Executing request " + httpget.getRequestLine());
CloseableHttpResponse response = httpclient.execute(httpget);
try {
System.out.println("----------------------------------------");
ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
#Override
public String handleResponse(
final HttpResponse response) throws ClientProtocolException, IOException {
int status = response.getStatusLine().getStatusCode();
if (status >= 200 && status < 300) {
HttpEntity entity = response.getEntity();
return entity != null ? EntityUtils.toString(entity) : null;
} else {
throw new ClientProtocolException("Unexpected response status: " + status);
}
}
};
String json= new String(httpclient.execute(httpget, responseHandler).getBytes("ISO-8859-1"),"UTF-8");
PrintWriter out = new PrintWriter("filename.txt");
out.println(json);
out.close();
System.out.println(json);
JSONObject obj = JSONObject.fromObject(json);
System.out.println(obj==null);
Sb newSB= (Sb)JSONObject.toBean(obj,Sb.class);
System.out.println(newSB==null);
System.out.println(newSB.IsUpdateForcibly);
System.out.println(newSB.Version);
System.out.println(newSB.ReleaseNote);
System.out.println(newSB.DownloadLink);
}
catch(Exception ex){
System.out.println(ex.getMessage());
}
finally {
response.close();
}
}
catch(Exception ex){
System.out.println(ex.getMessage());
}
finally {
httpclient.close();
}
System.out.println("end");
}
}
I had a similar problem. I solved it by adding "UTF-8"
String str= EntityUtils.toString(entity2);
to
String str= EntityUtils.toString(entity2,"UTF-8");
demo:
private static void sendPost() throws ClientProtocolException, IOException
{
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://127.0.0.1:8911/crr");
ArrayList<NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("crawlId", "123"));
nvps.add(new BasicNameValuePair("transType", "0"));
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
CloseableHttpResponse response2 = httpClient.execute(httpPost);
try {
System.out.println(response2.getStatusLine());
HttpEntity entity2 = response2.getEntity();
String str= EntityUtils.toString(entity2,"UTF-8");
System.out.println(str);
} finally {
response2.close();
}
}
This probably stems from a Unicode BOM character, a zero-width space in Unicode that is used in UTF-8, UTF-16LE, UTF-16BE at the beginning of a file to mark it as Unicode: \uFEFF. It is redundant, unneeded, and - as seen here - causes several problems.
It was replaced with a question mark, as the character encoding of the saved text could not represent the BOM character.
As #zhizhi mentioned, better safe the JSON as UTF-8. Still better is to remove the BOM.
PrintWriter out = new PrintWriter("filename.txt", "UTF-8");
json = json.replaceFirst("^\uFEFF", "");
Mind that removing the BOM poses a UTF-8 recognition problem for Notepad.
I've implemented a REST service on Glassfish server (GF4) and also a small client program to test it. But the server responds with a "Status 415 - Unsupported Media Type" error. Please have a look at my code and give me some hints.
Server code:
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.PathParam;
import javax.ws.rs.Consumes;
import javax.ws.rs.Path;
import javax.ws.rs.POST;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.MessagingException;
import javax.ws.rs.GET;
#Path("generic")
public class GenericResource {
#Context
private UriInfo context;
public GenericResource() {
System.out.println("Mediatype: "+MediaType.MULTIPART_FORM_DATA);
}
#Path("post")
#POST
#Consumes(MediaType.MULTIPART_FORM_DATA)
public void postXml(javax.mail.internet.MimeMultipart multiPart) {
try {
System.out.println(multiPart.getCount());
} catch (MessagingException ex) {
Logger.getLogger(GenericResource.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
This is my client code:
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.FileUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.HttpClientBuilder;
public class URLTester {
public static void main(String[] args) throws Exception {
URLTester http = new URLTester();
http.sendPost();
}
// HTTP POST request
private void sendPost() throws Exception {
String filename = "C:\\development\\java\\URLTester\\test.obx";
String xml = readXML(filename);
String url = "http://localhost:8080/RESTgarbio2oex/webresources/generic/post";
HttpClient httpclient = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(url);
// MultiPart POST
FileBody fb = new FileBody(new File(filename));
post.setHeader("Content-type", "multipart/form-data");
MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();
multipartEntity.addPart("file", fb);
multipartEntity.addTextBody("data", "Here is the data.");
post.setEntity(multipartEntity.build());
HttpResponse response = httpclient.execute(post);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String inputLine;
while ((inputLine = rd.readLine()) != null) {
System.out.println(inputLine.toString());
}
}
private String readXML(String xmlsource) {
String xml = "";
try {
File in = new File(xmlsource);
xml = FileUtils.readFileToString(in);
} catch (IOException ex) {
Logger.getLogger(URLTester.class.getName()).log(Level.SEVERE, null, ex);
}
return xml;
}
}
As you can see, I'm setting the content type to "multipart/form-data" and use a MultipartEntityBuilder to create the data to send. I also use the Apache HTTPClientBuilder class to post the data to the server.
On server side, the postXml method isn't called at all, even when my debug console says the constructer has been called and puts Mediatype: multipart/form-data as result.
If you need further infos, please let me know. I've googled so many sites, but haven't come across a solution for my problem. Thanks in advance.